Connector Synchronize REST API - automating the synchronization process
This guide will help you configure your Salesforce using the Connector Synchronize REST API so that it has the ability to automate the synchronization process between Salesforce and Jira.
The benefits of using this method include not requiring Jira credentials to log in every time and streamlining the process so that time is saved.
This method will require writing several Apex Classes, the codes of which will be provided below. Also provided are Unit Test Codes for the Create REST API.
This setup can only be used if your Jira instance is not behind a firewall.
Writing the Apex Class
This example will guide you on how to call the Synchronize REST API.
Before you write the Apex code that calls the remote URL, you have to allow Salesforce.com to make a call to the external server.
Log in into Salesforce.
Go to Your Name > Setup > Administration Setup > Security Controls > Remote Site Settings.
Click New Remote Site.
Enter the name of remote site.
Enter your Jira URL in Remote Site URL, e.g.:
http://yourjiraurl.com/
Click Save.
Once you are done, you can start writing the code. Here are the steps to write a Salesforce Apex Class.
Log in into Salesforce.
Go to Setup > App Setup > Develop > Apex Classes > New. (If you have already created this Apex Class, you do not need to do this step).
Paste the Apex Class parent code into the field.
Now, create another Apex Class (follow the same steps in Step 2) and paste the following code into the field:
global class JIRAConnectorWebserviceCalloutSync { @future (callout=true) WebService static void synchronizeWithJIRAIssue(String baseUrl, String systemId, String objectType, String caseId) { try { HttpRequest req = buildRequest(baseUrl, JIRA.username, JIRA.password, systemId, objectType, caseId); JIRA.sendRequest(req); } catch(System.CalloutException e) { System.debug(e); } } // Constructs request needed to synchronize a JIRA issue from provided parameters. public static HttpRequest buildRequest(String baseUrl, String username, String password, String systemId, String objectType, String caseId) { HttpRequest req = new HttpRequest(); String basicAuthHeader = JIRA.authHeader(username, password); String endpoint = getEndpoint(baseUrl, systemId, objectType, caseId); req.setHeader('Authorization', basicAuthHeader); req.setHeader('Content-Type', 'application/json'); req.setHeader('Content-Length', '0'); req.setEndpoint(endpoint); req.setMethod('PUT'); return req; } // Creates the endpoint to synchronize the issue from provided parameters. private static String getEndpoint(String baseUrl, String systemId, String objectType, String caseId) { return baseUrl + '/rest/customware/connector/1.0/' + systemId + '/' + objectType + '/' + caseId + '/issue/synchronize.json'; } }
Click Save.
WARNING
Please use this class with care. It can cause an infinite loop of Issue and Case synchronization if conflicted with Jira push updates.
Create a Jira Agent
It's important to create a custom profile called "Jira Agent" in Salesforce to avoid the problem of infinite loops.
Then we will assign a user to that profile and block it to execute the trigger. With this method, any Case updated from Jira will not trigger synchronization of issues back to Jira.
First, create a custom profile called "Jira Agent":
Log in to Salesforce.
Go to Setup > Administration Setup > Manage Users > Profiles > New Profile.
Choose Existing Profile to be: Custom: Support Profile
Fill in Profile Name with: "Jira Agent"
Click Save.
Second, assign the created profile to a specific user that will be used in the Connector:
Go to Setup > Administration Setup > Manage Users > Users
Create/Edit the user that is used as the Jira agent.
Use the created profile as the profile of the user.
Click Save.
Usage of the written class
Now that the class is ready to be used and it can be called in various ways including:
From within a Trigger
From a Synchronize JIRA Issue Button
From a Case Comment Trigger
Trigger
A Salesforce trigger can be used to automate the synchronization of Case with the Jira issue. It is an event-based process similar to Jira workflow, yet more flexible.
In this example, we want to automate Jira issue synchronization when a Case is updated:
Add a Trigger code to the Case Trigger.
Write Apex Class as described above.
Go to Setup > Application Setup > Customize > Cases > Triggers.
Create a New Trigger and replace the code with:
trigger SynchronizeWithJIRAIssue on Case (after update) { // Check whether current user is not JIRA agent so that we don't create an infinite loop. if (JIRA.currentUserIsNotJiraAgent()) { for (Case c : Trigger.new) { String objectType ='Case'; //Please change this according to the object type String objectId = c.id; // Calls the actual callout to synchronize with the JIRA issue. JIRAConnectorWebserviceCalloutSync.synchronizeWithJIRAIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId); } } }
Change the variable accordingly:
Click Save.
Now you are ready to use the trigger. Try creating a Case in Salesforce.com as a normal user and check if the JIRA Issue is synchronized.
Button
Other than the trigger, you can use the written Apex class for the button. Using this method, you can eliminate some processes, including:
The required Login page
The prompt page of choosing Project and Issue type.
Steps:
Write Apex Class as in above
Then, you can follow the steps in Configuring JIRA Issue Creation Button to create a button, there are slight changes in Step 8:Change the variables accordingly:
Now you are ready to test:
Create a new Case in Jira.
Click on the Synchronize JIRA Issue button.
You should see a pop-up window: "JIRA Issue should now be synchronized."
Check your JIRA instance to see whether the issue was created.
Case Comment Trigger
The Salesforce Case Comment Trigger can be used to automate the synchronization of Case Comments with Jira Issue comments.
In this example, we want to automate the Jira Issue comment synchronization when the Case is updated:
Add a Trigger code to Case Comment Trigger.
Write Apex Class as described above.
Go to Setup > Application Setup > Customize > Cases > Case Comments > Triggers.
Create New Trigger and replace the code with:
trigger CaseCommentSync on CaseComment (after insert, after update) { // Check whether current user is not JIRA agent so that we don't create an infinite loop. if (JIRA.currentUserIsNotJiraAgent()) { for (CaseComment cc : Trigger.new) { String objectType ='Case'; // Please change this according to the object type. String objectId = cc.ParentId; // Calls the actual callout to synchronize with the JIRA issue. JIRAConnectorWebserviceCalloutSync.synchronizeWithJIRAIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId); } } }
Change the variable accordingly:
Click Save.
Now you are ready to use the Case Comment trigger. Try creating a comment in a Salesforce Case as a normal user and check whether the JIRA Issue comment is synchronized.
Attachments
To automatically pull attachments from Salesforce to Jira, a separate trigger is required. Insert the code below:
For more information about pulling attachments, read this documentation.
Salesforce Files
To automatically pull Salesforce Files, another trigger is required. Insert the code below:
Unit Test to Synchronize REST API
Provided below is the Unit Test code for this class.
Do remember to also include the Fixtures and Mock Objects found on the Apex Class Parent Code page before using the following code:
Notes
If you are only using a Case Trigger and not using a Case Comment Trigger, synchronization will not initiate after adding or editing a comment unless the case fields have been modified.
In order for new or edited comments to automatically synchronize from Salesforce.com to JIRA, you first need to configure a Case Comment Trigger.