function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Santhosh Reddy 54Santhosh Reddy 54 

How to call rest apex public class from outside like jira webhook

Hi Mates,

I need a help on integrating salesforce to jira and viceversa without any plugin/addon/third party tools in developer edition

i was trying to push case from salesforce to jira as issue, and it is done using Callout working fine
But also need to push issue from jira to salesforce as case for this i created a webhook and created apex rest api class in salesforce and made that class as public from site, see below class

@RestResource(urlMapping='/JiraToSFRestTest/*')
global class JiraTOSFRestTestController {
  @HttpPost
    global static String doPost(String issueId,String issueKey, String projectId, String projectKey) {
        Case CaseObj = new Case();
        CaseObj.Status = 'Working';
        CaseObj.Origin = 'Web';
        CaseObj.Subject = issueId+'->'+issueKey;
        CaseObj.Description = projectId+'->'+projectKey;
        insert CaseObj;
        return CaseObj.Id;
    }
}


webhook url look like this
https://SaleforceSiteURL/services/apexrest/JiraToSFRestTest?issueId=${issue.id}&issueKey=${issue.key}&projectId=${project.id}&projectKey=${project.key}

I am executing this rest class from workbench then it is working fine but how to call this rest api class directly without login into salesforce from jira webhook

Don't know how to impletement can any one help on this with step by step preocedure please

Awaiting your responce
GovindarajGovindaraj
Hi Santhosh,

What have you done is looks fine for jira to salesforce integration.

I didn't get this line of you " but how to call this rest api class directly without login into salesforce from jira webhook" .... For this only you have created public site.

Thanks,
Govindaraj.S
Raj VakatiRaj Vakati
These are the steps ..
  1. Create a Rest API Service 
  2. Create public site using salesforce sites 
  3. Include this Apex class in sites 
  4. You can able to access this from the JIRA without login into Salesforce 


Refer this links 

http://www.wadewegner.com/2013/03/creating-anonymous-rest-apis-with-salesforce-com/
Santhosh Reddy 54Santhosh Reddy 54
Hi Rajamohan thank you for your reply i have followed the same but, only 1 step i couldnot understand properly i.e in the given refer url documentation, 15th point showing that post data to that apex rest api , But in my scenario am using Jira Webhook to send data and there in jira there is no post data section all i can do from there is i can hit a salesforce url with some parameters those i need to use as posted data in apex rest api the apex rest api class and Webhook URL also i added in email , how to achieve refer url 15th point in my scenario Thanking you , awaiting your responce
Santhosh Reddy 54Santhosh Reddy 54
Hi Govinda Thank you for your reply i mean to say that i created public site and made the apex rest api class as public but trying call that public apex class from jira webhook like this : *https://SaleforceSiteURL/services/apexrest/JiraToSFRestTest?issueId=${issue.id}&issueKey=${issue.key}&projectId=${project.id}&projectKey=${project.key} * but record is not inserting and no idea how to test or debug this for example same URL am calling from normal browser directly like this : https://saleforcesiteurl/services/apexrest/JiraToSFRestTest?issueId=111&issueKey=Hi&projectId=222&projectKey=Hello then also it is not inserting or no responce Awating your responce
GovindarajGovindaraj
Hi Santhosh,

Did u give access to the 'Apex class' & 'Case object' in 'Public AccessSettings' ?

Thanks,
Govindaraj.S
Santhosh Reddy 54Santhosh Reddy 54
yes i have given access to Case object and Apex class in public access settings [image: image.png] [image: image.png] https://siteURL/services/apexrest/JiraTOSFRestTestController?issueId=111&issueKey=Hi&projectId=222&projectKey=Hello
Santhosh Reddy 54Santhosh Reddy 54
When am using the apex rest url https://aletisanthu-developer-edition.ap4.force.com/services/apexrest/JiraToSFRestTest?issueId=111&issueKey=Hi&projectId=222&projectKey=Hello its showing error of [image: image.png] could you please let me know , did i miss anything?
GovindarajGovindaraj
Santhosh, Could you please attach the image, it's not visible here.
Avinash BarolaAvinash Barola
Hi,
Do you have any namespace in your org?
I am able to call REST API from JIRA Webhook using this url : https://complete-site-url/services/apexrest/hii//webhook/V1.

User-added image

Thanks
Santosh Kumar PSantosh Kumar P
@Santhosh Reddy 54

We are having the same issue while posting could you please let me know what was the resolution you did to achive this. If possible please share the code.
Sergii Grushai 25Sergii Grushai 25
Hello Santosh,

pease have a look at few code examples below,
taken from the blog "7 Ways to Integrate Salesforce and Jira (https://www.peeklogic.com/article/7-ways-to-integrate-salesforce-and-jira/)"


Create Jira Issue from Salesforce CRM:
@InvocableMethod(label='Create Jira Issue' description='Create Jira Issue from Salesforce.')

public static void createJiraIssue(List ids) {
try{
createIssue(ids);
}
catch(Exception ex){
System.debug('ERROR:' + ex.getMessage());
}
}
@Future(callout=true)
public static void createIssue(List ids){
List caseList = [SELECT Subject,Summary__c, Assignee__c, Reporter__c,Priority FROM Case WHERE id = :ids LIMIT 1];
System.debug('caseList::::::'+caseList);
if(caseList.size()>0){
String isssueSummary = caseList[0].Summary__c;
String issueAssignee = caseList[0].Assignee__c;
String issueReporter = caseList[0].Reporter__c;
Jira_Credential__c jiraCreds = [Select id, Jira_API_Token__c,Jira_Password__c,Jira_URL__c,Jira_UserName__c from Jira_Credential__c Limit 1];
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(jiraCreds.Jira_UserName__c+':'+jiraCreds.Jira_API_Token__c);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('content-type','application/json');
req.setHeader('accept','application/json');
//Set Method and Endpoint and Body
req.setMethod('POST');
//req.setHeader('cache-control','no-cache');
//Construct Endpoint
req.setBody('{ "fields": { "project": { "id": "10000" }, "summary": "'+isssueSummary+'", "issuetype": { "id": "10001" }, "assignee": { "id": "'+issueAssignee+'" }, "reporter": { "id": "'+issueReporter+'" }, "priority": { "id": "1" }, "labels": [ "bugfix", "blitz_test" ], "description": "description", "duedate": "2021-08-11" } }');                req.setEndpoint(jiraCreds.DemoNa__Jira_URL__c+'/rest/api/2/issue');
res = http.send(req);
System.debug('ResponseBody::'+res.getBody());
}
}
}



Get Jira issue details with Apex
public class JIRAWebserviceCallout {

public static void getJiraIssue(String IssueId){
try{
//Querying the custom settings to fetch the Jira credentials
Jira_Credential__c jiraCreds = [Select id, Jira_API_Token__c,Jira_Password__c,Jira_URL__c,Jira_UserName__c from Jira_Credential__c Limit 1];
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(jiraCreds.Jira_UserName__c+':'+jiraCreds.Jira_API_Token__c);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type','application/json');
req.setHeader('accept', 'application/json');
//Set Method and Endpoint and Body
req.setMethod('GET');
//Construct Endpoint
req.setEndpoint(jiraCreds.DemoNa__Jira_URL__c+'/rest/api/2/issue/'+IssueId);
res = http.send(req);
System.debug('ResponseBody::'+res.getBody());
}
catch(Exception ex){
System.debug('ERROR:' + ex.getMessage());
}
}
}



Add comment to Jira Issue in Salesforce CRM
public class JIRAWebserviceCallout {
public static void addCommentToJiraIssue(String IssueId){
try{
//Querying the custom settings to fetch the Jira credentials
Jira_Credential__c jiraCreds = [Select id, Jira_API_Token__c,Jira_Password__c,Jira_URL__c,Jira_UserName__c from Jira_Credential__c Limit 1];
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(jiraCreds.Jira_UserName__c+':'+jiraCreds.Jira_API_Token__c);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('content-type','application/json');
req.setHeader('accept', 'application/json');
//Set Method and Endpoint and Body
req.setMethod('POST');
//Construct Endpoint req.setEndpoint(jiraCreds.DemoNa__Jira_URL__c+'/rest/api/2/issue/'+IssueId+'/comment');
req.setBody('{"body": "This is a test comment " }');
res = http.send(req);
System.debug('ResponseBody::'+res.getBody());
}
catch(Exception ex){
System.debug('ERROR:' + ex.getMessage());
}
}
}