Connector Fetch REST API - Showing associated JIRA Issues in Salesforce Cases

This guide will help you configure your Salesforce using the Connector Fetch REST API so that it has the ability to display Jira information via the Visualforce page. The information will be called through the Fetch Endpoint provided by the Connector.

This method will require writing several Apex Classes, the codes of which will be provided below. Also provided are Unit Test Codes for the Fetch REST API.

 This setup can only be used if your Jira instance is not behind a firewall.

Writing the Apex Class

Step 1: Write the APEX controller

The Fetch REST API can be called from within an Apex Class. This example will guide you on how to call the Fetch REST API.

Before you write Apex code that calls a remote URL, you have to allow Salesforce to make a call to the external server.

  1. Log in into Salesforce.

  2. Go to Your Name > Setup > Administration Setup > Security Controls > Remote Site Settings.

  3. Add new remote site.

  4. Enter the name of remote site.

  5. Enter your Jira URL in Remote Site URL, e.g.: http://yourjiraurl.com/

  6. Click Save.

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

  1. Log in into Salesforce.

  2. 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).

  3. Now, create another Apex Class (follow the same steps in Step 2) and paste the following code into the field:

public class JIRAFetchIssuesController { private String issuesJson; // Constructor of this class. Note that a request will be sent to JIRA when this class is constructed. public JIRAFetchIssuesController(ApexPages.StandardController stdController) { Case theCase = (Case)stdController.getRecord(); this.issuesJson = getResultJson('Case', theCase.id); } // Sends request to JIRA and returns the request body which should be a valid JSON. private static String getResultJson(String objectType, String objectId) { try { HttpRequest req = buildRequest(JIRA.baseUrl, JIRA.username, JIRA.password, JIRA.systemId, objectType, objectId); HttpResponse res = JIRA.sendRequest(req); return res.getBody(); } catch(System.CalloutException e) { System.debug(e); return ''; } } // Constructs request needed to fetch JIRA issues from provided parameters. @testVisible private static HttpRequest buildRequest(String baseUrl, String username, String password, String systemId, String objectType, String objectId) { HttpRequest req = new HttpRequest(); String basicAuthHeader = JIRA.authHeader(username, password); String endpoint = getEndpoint(baseUrl, systemId, objectType, objectId); req.setHeader('Authorization', basicAuthHeader); req.setHeader('Content-Type','application/json'); req.setMethod('GET'); req.setEndpoint(endpoint); return req; } // Creates the endpoint to fetch the issue from provided parameters. private static String getEndpoint(String baseUrl, String systemId, String objectType, String objectId) { return baseUrl + '/rest/customware/connector/1.0/' + systemId + '/' + objectType + '/' + objectId + '/issue/fetch.json'; } public String getIssuesJSON() { return this.issuesJson; } public List<JIRAIssue> getIssues() { return (List<JIRAIssue>)JSON.deserialize(this.issuesJson , List<JIRAIssue>.class); } // JIRA Issue Object. @testVisible class JIRAIssue { public String summary { get; } public String project { get; } public String reporter { get; } public String key { get;} public String status { get; } public String resolution { get; } public String url { get; } public String type { get; } public String assignee { get; } public String description { get; } public String priority { get; } public String due_date { get; } } }



Copying the above code and pasting it directly into the field may result in additional, unwanted characters being included. To avoid this from happening, we recommend pasting the code into a text editor that accepts plain text (e.g. Notepad.exe) and then recopying it again before pasting it into the field.

Since the example code above uses Case for the Salesforce Object, remember to change the code where it references "Case" to the the Salesforce Object you are using.

For example, replace "Case" according to your Object type in the following line of code:

getResultJson('Case', theCase.id)

would be changed to:

getResultJson('Account', theAccount.id)



Using the REST API to fetch JIRA issue date data will return the value in the epoch date format. You will need to manually convert the value to a more human readable format. View our KB article for more information.

Step 2: Create the Visualforce page

Now that the Apex controller has been written, we can use it in Visualforce page. Let's create a Visualforce page:

  1. Go to Setup > App Setup > Develop > Pages > New

  2. In "label" field: FetchJIRAIssues

  3. In "name" field: FetchJIRAIssues

  4. In "Visualforce Markup", paste the code below.

  5. Click Save.
     



Copying the above code and pasting it directly into the field may result in additional, unwanted characters being included. To avoid this from happening, we recommend pasting the code into a text editor that accepts plain text (e.g. Notepad.exe) and then recopying it again before pasting it into the field.

You can modify the code accordingly, based on what field do you want to show. 

Step 3: Add the created Visualforce page to the Case layout

Finally, we need to use the Visualpage created in the layout. In our example, we will associate the page in Case Layout:

  1. Go to Setup > App Setup > Customize > Cases > Page Layout

  2. In any of the page layout, add New Section.

  3. Add the FetchJIRAIssues Visualforce page to that section.

  4. Click Save.


     

Step 4: Create a Jira Issue based on a Salesforce Case

To see the result, create a Jira Issue based on a Salesforce Case and you will see the page in the Case.

Set security permission for user access to the created page

For the created page to be accessible by certain user in your Salesforce organization, you will need to enable profiles to access the created class and page.

For the created Apex Class:

  1. Go to Setup > Develop > Apex Classes.

  2. In your JIRAFetchIssues class, click on Security.

  3. Enable the profile of users that you want to give access to.

For VisualForce page:

  1. Go to Setup > Develop > Pages.

  2. In your JIRAFetchIssues page, click on Security.

  3. Enable the profile of users that you want to give access to.

Now your created page will be accessible to permitted user profiles.

Unit test for Fetch 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: