Servicenow and Jira comment update sync through webhook

attanhes
Tera Guru

I am trying to integrate ServiceNow with Jira and worked out how to sync new ticket create in Jira when new incident created in ServiceNow through Scripted REST API. It works fine, but i still could not figure out how update comments in both direction.

find_real_file.png

I tried webhook, connection works fine between ServiceNow and Jira. When Jira tickets updated new response sent to SNOW and I can see it under logs in JSON format. but I don't know how to get values from this response body and fetch into relevant ServiceNow tickets under comment field.

I really appreciated if someone can help me here:

Json response body:

{"timestamp":1589671258662,"webhookEvent":"comment_created","comment":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/issue/10060/comment/10000","id":"10000","author":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/user?accountId=5ea23ec8c0b45b0c17a88675","accountId":"5ea23ec8c0b45b0c17a88675","avatarUrls":{"48x48":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=48&s=48","24x24":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=24&s=24","16x16":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=16&s=16","32x32":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=32&s=32"},"displayName":"Han Ata","active":true,"timeZone":"Australia/Sydney","accountType":"atlassian"},"body":"I have broken mouse","updateAuthor":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/user?accountId=5ea23ec8c0b45b0c17a88675","accountId":"5ea23ec8c0b45b0c17a88675","avatarUrls":{"48x48":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=48&s=48","24x24":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=24&s=24","16x16":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=16&s=16","32x32":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/5ea23ec8c0b45b0c17a88675/e4ab785d-60d3-4f7e-8627-650abcd42289/128?size=32&s=32"},"displayName":"Han Ata","active":true,"timeZone":"Australia/Sydney","accountType":"atlassian"},"created":"2020-05-17T09:20:58.662+1000","updated":"2020-05-17T09:20:58.662+1000","jsdPublic":true},"issue":{"id":"10060","self":"https://Jirasnowintegration.atlassian.net/rest/api/2/10060","key":"CAD-58","fields":{"summary":"Test test","issuetype":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/issuetype/10001","id":"10001","description":"Functionality or a feature expressed as a user goal.","iconUrl":"https://Jirasnowintegration.atlassian.net/secure/viewavatar?size=medium&avatarId=10315&avatarType=issuetype","name":"Story","subtask":false,"avatarId":10315},"project":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/project/10000","id":"10000","key":"CAD","name":"CAD","projectTypeKey":"software","simplified":false,"avatarUrls":{"48x48":"https://Jirasnowintegration.atlassian.net/secure/projectavatar?pid=10000&avatarId=10409","24x24":"https://Jirasnowintegration.atlassian.net/secure/projectavatar?size=small&s=small&pid=10000&avatarId=10409","16x16":"https://Jirasnowintegration.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10000&avatarId=10409","32x32":"https://Jirasnowintegration.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10000&avatarId=10409"}},"assignee":null,"priority":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/priority/3","iconUrl":"https://Jirasnowintegration.atlassian.net/images/icons/priorities/medium.svg","name":"Medium","id":"3"},"status":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/status/10000","description":"","iconUrl":"https://Jirasnowintegration.atlassian.net/","name":"Backlog","id":"10000","statusCategory":{"self":"https://Jirasnowintegration.atlassian.net/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To Do"}}}}}

 

 

1 ACCEPTED SOLUTION

try below

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	var body = JSON.stringify(request.body.data);
	var parser = new JSONParser();
	var parameterArr = parser.parse(body);

	var gr = new GlideRecord("incident");
	gr.addQuery("u_jira_issueid", parameterArr.issue.key);
	gr.query();
	if (gr.next()) {
		gr.work_notes = 'Commented by: ' + parameterArr.comment.author.displayName + '\nComment: '+ parameterArr.comment.body;
		gr.update();
		//gs.log(parameterArr.comment.body);
	}

})(request, response);

View solution in original post

27 REPLIES 27

Thank you so much, it worked like a magic in one way.

When comment added to Jira ticket then ServiceNow corresponding ticket got updated with latest comment, but when comment added to ServiceNow ticket it did not sync to Jira ticket.

Do you have any clues as why comments are not sync to Jira from Servicenow?

You have to create business rule to kick off jira add comments integration. It will be similar as create.

Thanks a lot I will create another business rule to sync comments to Jira ticket.

Hi Mike,

I still could get this working to sync comment to Jira issue when new comment added into ServiceNow incident. I created a new business rule to trigger when new comment added to ServiceNow incident but i could not get the logic correct to find the corresponding Jira ticket to fetch latest comment. Tried many way still no luck. I really appreciated if you could help me here.

find_real_file.png

I created below after business rule to sync comment to Jira issue but I could not get logic correct to find the corresponding ticket to sync latest comment to Jira issue:

find_real_file.png

(function executeRule(current, previous /*null when async*/) {


	//-- update CAD-62, id -10064, INC0010080, https://jiraxxx.atlassian.net/browse/CAD-62
	
var body = "{\"fields\":{\"project\":{\"key\":\"CAD\"},\"summary\": " + "\"" + current.short_description + "\",\"issuetype\": {\"name\":\"Story\"},\"id\":\"10060\",\"key\":\"CAD-58\",\"update\":{\"comment\":[{\"add\":{\"body\":\"It is time to finish this task\"}}]},\"customfield_10028\":" + "\"" + current.number + "\",\"description\":{\"type\":\"doc\",\"version\":1,\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":" + "\"" + current.description + "\",\"type\":\"text\"}]}]}}}";
	
	var request = new sn_ws.RESTMessageV2();
	request.setEndpoint('https://jixxxxxx.atlassian.net/rest/api/3/issue');
	request.setHttpMethod('POST');
	request.setRequestBody(body);
	
	var user= 'xxxxx@gmail.com';
	var password = 'xxxx';//API token ID
		
	request.setBasicAuth(user,password);
	request.setRequestHeader("Accept","application/json");
	request.setRequestHeader("Content-Type","application/json");
	
		
	var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();


gs.log("HTTPCODE:"+httpStatus+"BODY"+response.getBody());
	gs.log(response.getBody());

	
})(current, previous);

 

 

Jira issue create business rule which works as expected:

find_real_file.png

(function executeRule(current, previous /*null when async*/) {

	
		var body = "{\"fields\":{\"project\":{\"key\":\"CAD\"},\"summary\": " + "\"" + current.short_description + "\",\"issuetype\": {\"name\":\"Story\"},\"customfield_10028\":" + "\"" + current.number + "\",\"description\":{\"type\":\"doc\",\"version\":1,\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":" + "\"" + current.description + "\",\"type\":\"text\"}]}]}}}";
	
	var request = new sn_ws.RESTMessageV2();
	request.setEndpoint('https://jiraxxx.atlassian.net/rest/api/3/issue');
	request.setHttpMethod('POST');
	request.setRequestBody(body);
	
	var user= 'xxx@gmail.com';
	var password = 'xxx';//API token ID
		
	request.setBasicAuth(user,password);
	request.setRequestHeader("Accept","application/json");
	request.setRequestHeader("Content-Type","application/json");
	
	/*var response = request.execute();
	gs.log(response.getBody());*/
	
	var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var obj = JSON.parse(responseBody); //define JSON parsing for the response JSON file to decode
	var targetIncidentID = obj.key;///This value comes from output Json
current.u_jira_issueid = targetIncidentID;
current.update();
gs.log("HTTPCODE:"+httpStatus+"BODY"+response.getBody());
gs.log("Jira targetIncidentID: "+targetIncidentID);
	
})(current, previous);

Change business rule to async and add below condition to business rule

Condition:

additional comments changes AND

jira issueID IS NOT EMPTY

and on advance section of condition add

 gs.getSession().isInteractive() 

Script:

(function executeRule(current, previous /*null when async*/ ) {
var body = '{"body": "' + current.comments.toString()+'"}';
    var request = new sn_ws.RESTMessageV2();
    request.setEndpoint('https://jixxxxxx.atlassian.net/rest/api/3/issue' + current.u_jira_issueid + '/comment');
    request.setHttpMethod('POST');
    request.setRequestBody(body);

    var user = 'xxxxx@gmail.com';
    var password = 'xxxx'; //API token ID

    request.setBasicAuth(user, password);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json");

    var response = request.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();

    gs.log("HTTPCODE:" + httpStatus + "BODY" + response.getBody());
    gs.log(response.getBody());

})(current, previous);