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

on business rule you need

var comments = {};
comments.body = current.comments;

r.setStringParameterNoEscape('commentBody', JSON.stringify(comments));

 

Hi Mike,

I really appreciated your last response.

I was really waiting for the weekend to work on this issue as week days got really busy with other works.

After many attempts, of cause with your help finally I was able to send fixed text to Jira ticket through this BR when new comment added to ticket. So that means JIRA add comment API is correct.

But I was never able figure out how to send last comments from ServiceNow tickets to correspondent JIRA issue id. Looks like comment send JSON format and end_point are not correct based on error logs.

find_real_file.png

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

	var comments = {};
//comments.body = current.comments;
comments = current.comments.getJournalEntry(1);
	
 try { 
 var r = new sn_ws.RESTMessageV2('Jira Testing Demo', 'Post');
 r.setStringParameterNoEscape('issueKey', 'CAD-64');
 r.setStringParameterNoEscape('base_endpoint', 'https://jirxxxxx.atlassian.net'); //works
//r.setStringParameterNoEscape('base_endpoint', ('https://jiraxxxxx.atlassian.net'+ current.u_jira_issueid + '/comment')); //Not works
//r.setStringParameterNoEscape('commentBody',"{\"body\": " + "\"" + current.comments.getJournalEntry(1) + "\"}"); //Not works
r.setStringParameterNoEscape('commentBody',"{\"body\":\"Just testing comment sync...\"}"); //works
//r.setStringParameterNoEscape('commentBody', JSON.stringify(comments)); //Not working
 
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}
	gs.log("HTTPCODE:" + httpStatus + "BODY:" + current.number + response.getBody());
    //gs.log(response.getBody());
	gs.log(message.getBody());
	gs.log("LAST COMMENT: " + current.comments.getJournalEntry(1));
	//gs.addInfoMessage("LAST COMMENT: " + current.comments);
	gs.log(r.getBody());
	gs.log("last comments through variable:" + comments);
	
})(current, previous);

 

 

Please point me right direction.

 

try below

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


    var comments = {};
    comments.body = current.comments.getJournalEntry(1).toString().replace(/\[\/?code\]/mg, "");

    try {
        var r = new sn_ws.RESTMessageV2('Jira Testing Demo', 'Post');
        r.setStringParameterNoEscape('issueKey', 'CAD-64');
        //r.setStringParameterNoEscape('base_endpoint', 'https://jirxxxxx.atlassian.net'); //works
        r.setStringParameterNoEscape('base_endpoint', ('https://jiraxxxxx.atlassian.net/' + current.u_jira_issueid + '/comment'));
        //r.setStringParameterNoEscape('commentBody',"{\"body\": " + "\"" + current.comments.getJournalEntry(1) + "\"}"); //Not works
        //r.setStringParameterNoEscape('commentBody', "{\"body\":\"Just testing comment sync...\"}"); //works
        r.setStringParameterNoEscape('commentBody', JSON.stringify(comments));

        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
    }
    gs.log("HTTPCODE:" + httpStatus + "BODY:" + current.number + response.getBody());
    //gs.log(response.getBody());
    gs.log(message.getBody());
    gs.log("LAST COMMENT: " + current.comments.getJournalEntry(1));
    //gs.addInfoMessage("LAST COMMENT: " + current.comments);
    gs.log(r.getBody());
    gs.log("last comments through variable:" + comments);

})(current, previous);

Many thanks always. I was able to worked out current comment sync to the issue id(CAD-64) which is in the script, but still struggling to map with current issue ID. I tried to follow the same logic which applied to sync comment by adding new variable and call that into "issueKey" but still no luck.

find_real_file.png

 

Please provide some guidance.

 

 

(function executeRule(current, previous /*null when async*/ ) {
    var comments = {};
    //comments.body = current.comments.getJournalEntry(1).toString().replace(/\[\/?code\]/mg, "");//Not working
	comments.body = current.comments.getJournalEntry(1);
	
	var issueKey = {};
	issueKey.key = current.u_jira_issueid;

    try {
        var r = new sn_ws.RESTMessageV2('Jira Testing Demo', 'Post');
        //r.setStringParameterNoEscape('issueKey', 'CAD-64');//works
		r.setStringParameterNoEscape('issueKey', 'issueKey');
		//r.setStringParameterNoEscape('issueKey', '');
        r.setStringParameterNoEscape('base_endpoint', 'https://jirxxxx.atlassian.net'); //works
		//r.setStringParameterNoEscape('base_endpoint', ('https://jixxxx.atlassian.net/' + issueKey + '/comment'));//Not working 
        r.setStringParameterNoEscape('commentBody', JSON.stringify(comments));

        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
    }
    gs.log("HTTPCODE:" + httpStatus + "BODY:" + current.number + response.getBody());
    gs.log(message.getBody());
	gs.log("last comments through variable:" + comments.body);
	gs.log("IssueKey:" + issueKey.key);
    //gs.log("LAST COMMENT: " + current.comments.getJournalEntry(1));
    //gs.log(r.getBody());
 
})(current, previous);

 

Looks like I am making mistakes one after the other. Many thanks