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 very much for your response, but I am still getting errors and comments are not sync to Jira issue. I can see below errors on the logs.

find_real_file.png

This is my Rest Message endpoints.

POST

find_real_file.png

 

GET

find_real_file.png

 

 

Hi Mike,

I just noticed that there is an error on the BR line 5 as / missing at the end of URL and changed to: 

request.setEndpoint('https://jixxxxxx.atlassian.net/rest/api/3/issue/' + current.u_jira_issueid + '/comment');

but I am still getting errors:

Also I checked Jira endpoint and it's now correct and giving record data on Json format on browser but ServiceNow throw an error and comments are not get sync to Jira. Any clue??

Jira end point:

https://jixxxx.atlassian.net/rest/api/3/issue/CAD-62/comment

 

Below is what I use and works fine.

find_real_file.png

You can create same REST Message and use "Preview Script Usage" on that REST message to get code. Use that code in your business rule. Let me know what you get.

find_real_file.png

also log body using

gs.log(request.getBody());

I really appreciated your response.

After I setup my POST settings as your said, now I am getting correct HTTP status =201 on POST when click on test.

I think, i am doing something really wrong in BR as I still could not get this worked out eventhough i copied exact script from POST "Preview Script usage" into my business rule. Now I am getting some errors which i can not track down. I even created new business rule from scratch and tried many ways but still no luck.

Error logs:

{"errorMessages":["Unexpected character ('&' (code 38)): was expecting double-quote to start field name\n at [Source: org.apache.catalina.connector.CoyoteInputStream@657ab0d0; line: 1, column: 3]"]}

New business rule and error logs. I changed the line 7 highlighted in yellow by converting Json format to string.

find_real_file.png

 

REST Message - POST "Preview Script Usage" :

try { 
 var r = new sn_ws.RESTMessageV2('Jira Testing Demo', 'Post');
 r.setStringParameterNoEscape('issueKey', 'CAD-64');
 r.setStringParameterNoEscape('base_endpoint', 'https://jiraxxxxx.atlassian.net');
 r.setStringParameter('commentBody', '{
"body":"This is a comment regard..."
}');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}