Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.All Apex Class codes provided are also provided on our Bitbucket Repository for your reference.

(warning) This  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

...

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.:

...

  1.  http://yourjiraurl.com/

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

    • Paste

...

...

    •  into the field.

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

Code Block
languagejava
 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; }

    }
}

...

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.
     

Code Block
 <apex:page standardController="Case" extensions="JIRAFetchIssuesController" >
    <!-- Uncomment next line if you want to render the returned JSON object -->
    <!-- {!IssuesJSON}-->
 
    <apex:pageblock title="JIRA Issues" rendered="{!Issues.size > 0}">
    <!-- Create table for JIRA issues. With  -->
	    <apex:pageBlockTable value="{!Issues}" var="issue">
		    <!-- Issue Key column -->
		    <apex:column headerValue="Issue Key">
		        <!-- Make it link to JIRA Issue -->
		        <apex:outputLink value="{!issue.url}" target="_blank">{!issue.key}</apex:outputLink>
		    </apex:column>
		    <!-- Issue Resolution column -->
		    <apex:column headerValue="Resolution" value="{!issue.resolution}"/>
		    <!-- Issue Status column -->
		    <apex:column headerValue="Status" value="{!issue.status}" />
		    <!-- Issue Assignee column -->
		    <apex:column headerValue="Assignee" value="{!issue.assignee}" />
	    </apex:pageBlockTable>
    </apex:pageblock>
 
    <apex:pageblock title="JIRA Issues" rendered="{!Issues.size == 0}">
        <apex:outputLabel >No JIRA Issue associated with this Case</apex:outputLabel>
    </apex:pageblock>
 
</apex:page>

...

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.

...


  1. Image Added

     

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.Image Removed

...

Set security permission for user access to the created 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.

...