Older Versions

This documentation relates to the Salesforce and JIRA connector 5.0
If you are using Salesforce and JIRA connector 4.4.7 or earlier visit the Salesforce and JIRA connector 4.x documentation home page
Skip to end of metadata
Go to start of metadata

In addition to endpoints stated here. The connector provides the ability to create JIRA Issue from RESTful webservice which can be used in Apex class.

Connector RESTful Webservice definition

Note
This setup can only be used if your JIRA is NOT behind firewall

This webservice can be described as:

Title Item
HTTP Method POST
URL <JIRA URL>/rest/customware/connector/1.0/{systemId}/{objectType}/{objectId}/issue/create.{accept}
Produces APPLICATION_JSON
Consumes APPLICATION_JSON
Data Input {
"project" : "<project key>",
"issueType" : "<issue type>",
}

Using cURL, it can be constructed like this:

curl -i -H "Content-Type: application/json" -H "Authorization: Basic <64bit encoded username and password>" -X POST https://example.com/rest/customware/connector/1.0/1/Case/50090002N5r6/issue/create.json -d'{"project":"SFDC", "issueType":"1"}'

Detail Description

Connector provides three actions that can be performed in calling it's RESTful webservice, namely:

  1. Create
  2. Synchronize
  3. Fetch

They are all using similar patterns of endpoint
Endpoint: <JIRA URL>/rest/customware/connector/1.0/{systemId}/{objectType}/{objectId}/issue/{action}.{accept}

Action HTTP method Procudes {action}
Example Data Input
Create POST APPLICATION_JSON
create https://example.com/rest/customware/connector/1.0/1/Case/50090002N5r6/issue/create.json
"project" : "<project key>", 
"issueType" : "<issue type>", 
}
Synchronize PUT APPLICATION_JSON
synchronize https://example.com/rest/customware/connector/1.0/1/Case/50090002N5r6/issue/synchronize.json -
Fetch GET APPLICATION_JSON
fetch https://example.com/rest/customware/connector/1.0/1/Case/50090002N5r6/issue/fetch.json -

Writing the Apex Class

This webservice can be called from within Apex Class. This example will guide you on how to call "create" API.

Before you write Apex code that calls remote URL, you have to specify 

Login into Salesforce

  1. Go to Your Name -> Setup -> Administration Setup -> Security Controls -> Remote Site Settings
  2. Add new remote site
  3. Enter the name of remote site
  4. Enter your JIRA URL in Remote Site URL. e.g. http://yourjiraurl.com
  5. Click Save

Once you are done, you can start writing the code. Here are the steps to write Salesforce Apex Class.

  1. Login into Salesforce
  2. Go to Setup -> App Setup -> Develop -> Apex Classes -> New
  3. Paste this code into the field:
    global class JIRAConnectorWebserviceCallout {
        @future (callout=true)
        WebService static void createIssue( String jiraURL, String systemId, String objectType, String objectId, String projectKey, String issueType) {
    
            //Set your username and password here
            String username = 'yourJIRAusername';
            String password = 'yourJIRApassword';
    
            //Construct HTTP request and response
            HttpRequest req = new HttpRequest();
            HttpResponse res = new HttpResponse();
            Http http = new Http();
    
            //Construct Authorization and Content header
            Blob headerValue = Blob.valueOf(username+':'+password);
            String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
            req.setHeader('Authorization', authorizationHeader);
            req.setHeader('Content-Type','application/json');
    
            //Construct Endpoint
            String endpoint = jiraURL+'/rest/customware/connector/1.0/'+systemId+'/'+objectType+'/'+objectId+'/issue/create.json';
    
            //Set Method and Endpoint and Body
            req.setMethod('POST');
            req.setEndpoint(endpoint);
            req.setBody('{"project":"'+projectKey+'", "issueType":"'+issueType+'"}');
    
            try {
               //Send endpoint to JIRA
               res = http.send(req);
            } catch(System.CalloutException e) {
                System.debug(res.toString());
            }
        }
    
    }
    
  4. Change the variables accordingly:
    Variables Details
    username Your JIRA username
    password Your JIRA password
  5. Click Save.
Please be careful
Please use this class with care. It can cause infinite loop of Issue and Case creation if conflicted with JIRA "Push to Remote System" WORKFLOW.

The Usage of written class

Now that the class is ready to be used and it can be called in various ways including:

  1. From within a Trigger
  2. From "Create JIRA Issue" Button. As a replacement of: Configuring JIRA Issue Creation Button

Trigger

Salesforce Trigger can be used to automate the creation of JIRA issue. It is event-based process similar to JIRA workflow, yet more flexible. In this example, we want to automate JIRA issue creation when Case is created:

Very important to note:
We will create a custom profile called 'JIRA Agent' in Salesforce to avoid the problem of infinite loop. Then we will assign a user to that profile and block it to execute the trigger. Using this way, any Case created from JIRA will not trigger Creation of Issue back to JIRA.

First, create custom profile called 'JIRA Agent':

  1. Login to Salesforce
  2. Go to Setup -> Administration Setup -> Manage Users -> Profiles -> New Profile.
  3. Choose 'Existing Profile' to be: Custom: Support Profile
  4. Fill in 'Profile Name' with : JIRA Agent
  5. Click Save

Second, assign the created profile to specific user that will be used in JIRA Connector:

  1. Go to Setup -> Administration Setup -> Manage Users -> Users
  2. Create/Edit the user that is used as JIRA agent.
  3. Use the created profile as profile of the user.
  4. Save
    Make sure that this user is used in Application Links authentication in JIRA.

Third, add Trigger code to Case Trigger.

  1. Write Apex Class as described above
  2. Go to Setup -> Application Setup -> Customize -> Cases -> Triggers.
  3. Create New Trigger and replace the code with:
    trigger CreateIssue on Case (after insert) {
    
        //Identify profile name to be blocked from executing this trigger
        String JIRAAgentProfileName = 'JIRA Agent';
        List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];
    
        //Check if specified Profile Name exist or not
        if(!p.isEmpty())
        {
            //Check if current user's profile is catergorized in the blocked profile
            if(UserInfo.getProfileId()!= String.valueOf(p[0].id))
            {
                for (Case c : Trigger.new) {
    
                        //Define parameters to be used in calling Apex Class
                        String jiraURL = 'http://jira.example.com';
                        String systemId = '4';
                        String objectType ='CASE';
                        String objectId = c.id;
                        String projectKey = 'SFDC';
                        String issueType = '1';
                        //Execute the trigger
                        JIRAConnectorWebserviceCallout.createIssue(jiraURL, systemId ,objectType,objectId,projectKey,issueType);
                }
            }
        }
    }
    
  4. Change the variables accordingly:
    Variables Details
    jiraURL Your JIRA URL
    systemId Your system Id in Connection. You should check this in JIRA under Connection
    objectType Salesforce object that is used in Connection. In our example, it is Case
    objectId The id of the object. It varies in every operation. Therefore, it should be object.id
    projectKey Under which JIRA Project you want to create the issue.
    issueType standard JIRA issue type values range from '1' to '4' . '1' is for Bug and so on
  5. Click Save

Now you are ready to use the trigger.
Try out:
Create Case in Salesforce as normal user and check if JIRA Issue is created.

Button

Other than trigger, you can use the written Apex class for button. Using this method, you can eliminate some processes, including:

  1. The required Login page
  2. The prompt page of choosing Project and Issue type.

Steps:

  1. Write Apex Class as in above
  2. Then, you can follow the steps in Configuring JIRA Issue Creation Button to create a button, there are slight changes in Step 8:
    Label Create JIRA Issue
    Name Create_JIRA_Issue (this is auto-populated when you add a new label)
    Display Type Detail Page Button
    Behaviour Execute Javascript
    Content Source On Click Javascript
    Content
    {!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
    sforce.apex.execute("JIRAConnectorWebserviceCallout","createIssue", {jiraURL:"http://jira.example.com" , systemId:"4" , objectType:"Case",objectId:"{!Case.Id}",projectKey:"SFDC",issueType:"1"});
    window.alert("JIRA Issue should be created.");
    
  3. Change the variables accordingly:
    Variables Details
    jiraURL Your JIRA URL
    systemId Your system Id in Connection. You should check this in JIRA under Connection
    objectType Salesforce object that is used in Connection. In our example, it is Case
    objectId The id of the object. It varies in every operation. Therefore, it should be object.id
    projectKey Under which JIRA Project you want to create the issue.
    issueType standard JIRA issue type values range from '1' to '4' . '1' is for Bug and so on

Now you are ready to test:

  1. Create new Case in JIRA
  2. Click on Create JIRA Issue button
  3. Till you see the pop up says: JIRA Issue should be created
  4. Check your JIRA if the issue is created

Have fun!

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.