0

Am Integrating salesforce with Jira. I want to save the Jira URL in salesforce in which I created a custom field in internalrequest__C. I searched for some documentation from JIRA REST API and when you created a ticket from using POST you will get the response with the URL as an example from the same article:

{
    "id": "10000",
    "key": "TST-24",
    "self": "http://www.example.com/jira/rest/api/2/issue/10000"
}

but when I tried to get the "self" to be displayed in my custom field it always shows empty, IDK if it is something wrong with my code, can someone help me or explain to me how to complete this task?

My code:

public class jira_Insert {
    @future(callout = true)
    public static void createTicket(String project_value,String summary_value, String description_value
    ,String issue_type_value){

string username = 'username';

string password = 'password'; //the security token from Atlassian account profile --> Manage your Account-->Security-->API token-->Create and manage API tokens--> Manage your account-->API tokens
String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + password));

String url = 'URL';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/json;charset=utf-8');
req.setHeader('Accept','application/json');
req.setHeader('Authorization', auth_header);
req.setHeader('X-Atlassian-Token', 'nocheck');
req.setMethod('POST');
req.setEndpoint(url);

req.setBody('{ "fields": { "project": { "key": "'+project_value+'" }, "summary": "'+summary_value+'", "issuetype": { "name": "'+issue_type_value+'" },  "description": "'+description_value+'" } }');  

try {
    HttpResponse res = http.send(req); 
   system.debug(res);
      Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
            String jiraSelf = (String) m.get('self');
            update new Internal_Request__c (Jira_URL__c = jiraSelf); 
    
} catch (Exception e) {
    system.debug('Error: ' + e);
}
    }
}
3
  • 2
    You need to pass the Salesforce Id of the Internal_Request__c along with URL to update the record.
    – Saroj Bera
    Jul 18, 2022 at 18:11
  • As an aside to your question, lots of out of the box options for JIRA/SF integration: salesforce.com/solutions/small-business-solutions/integrations/…
    – identigral
    Jul 18, 2022 at 20:16
  • my bad the URL that I getting is JSON and not a normal, one, but I have this question about how I make the connectivity bidirectional?, so I can make modifications from both sites and they will display the information. will I need to code in Jira? or everything is going to be only in salesforce? using the API? Jul 18, 2022 at 23:07

1 Answer 1

0

I made the modifications in order to save the URL to a specific custom field when I created the issue to a ticket in the try I added:

try {
     HttpResponse res = http.send(req);
 
     system.debug(res);

     Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(res.getBody()); // I map the response that I get when I create the the ticket 

     String jira_key = (String) m.get('key'); // I set a variable to get the key for the response

     string link = 'https://yourattlassiandomain.net/browse/'+jira_key+'';   // I use the URL when you open a specific issue in Jira and I place the key with the key that I get for the response 
                 
      irst.URL_Jira__c = link; // I assigned the variable to the specific field 
      system.debug(link);
      updateRecord.add(irst);
      system.debug(irst);
      system.debug(req);
    
 } catch (Exception e) {
       system.debug('Error: ' + e);
     }
   }
 update updateRecord;
1
  • 1
    N.B. eating the exception in the catch block is not the best practice; you should also be checking the HttpResponse statusCode
    – cropredy
    Jul 21, 2022 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .