- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2020 03:09 AM
I have business rule and REST messages setup to sync incident record from SNOW to Jira. When record created in Jira response comes with Jira record number as in Json format. Can someone help me to get the value from response body into field?
This is my script:
(function executeRule(current, previous /*null when async*/) {
var body = "{\"fields\":{\"project\":{\"key\":\"CAD\"},\"summary\": " + "\"" + current.short_description + "\",\"issuetype\": {\"name\":\"Story\"},\"description\":{\"type\":\"doc\",\"version\":1,\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":" + "\"" + current.description + "\",\"type\":\"text\"}]}]}}}";
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://xxx.atlassian.net/rest/api/3/issue');
request.setHttpMethod('POST');
request.setRequestBody(body);
var user= 'xxxx';
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.result.sys_id;
current.u_jira_issueid = targetIncidentID;
current.update();
gs.log("HTTPCODE:"+httpStatus+"BODY"+response.getBody());
gs.log("Jira targetIncidentID: "+targetIncidentID);
})(current, previous);
I am getting below response as in Jason from Jira when record created.
HTTPCODE:201BODY{"id":"10045","key":"CAD-43","self":"https://xxxxxx.atlassian.net/rest/api/3/issue/10045"}
I wanted get that "CAD-43" number into ServiceNow incident field called (u_jira_issueid).
But I am getting below errors on the logs.
Can someone please help me.
Solved! Go to Solution.
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2020 03:32 AM
HI,
Use this code:
var obj = '{"id":"10045","key":"CAD-43","self":"https://xxxxxx.atlassian.net/rest/api/3/issue/10045"}';
var str = JSON.parse(obj);
gs.print(str.id); // HEre i get 10045
So your final code is:
(function executeRule(current, previous /*null when async*/) {
var body = "{\"fields\":{\"project\":{\"key\":\"CAD\"},\"summary\": " + "\"" + current.short_description + "\",\"issuetype\": {\"name\":\"Story\"},\"description\":{\"type\":\"doc\",\"version\":1,\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":" + "\"" + current.description + "\",\"type\":\"text\"}]}]}}}";
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://xxx.atlassian.net/rest/api/3/issue');
request.setHttpMethod('POST');
request.setRequestBody(body);
var user= 'xxxx';
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.id;
current.u_jira_issueid = targetIncidentID;
current.update();
gs.log("HTTPCODE:"+httpStatus+"BODY"+response.getBody());
gs.log("Jira targetIncidentID: "+targetIncidentID);
})(current, previous);
Thanks,
Ashutosh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2020 03:24 AM
Hi,
var targetIncidentID = obj.HTTPCODE['id'];
Try this.
Thanks,
Ashutosh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2020 03:32 AM
HI,
Use this code:
var obj = '{"id":"10045","key":"CAD-43","self":"https://xxxxxx.atlassian.net/rest/api/3/issue/10045"}';
var str = JSON.parse(obj);
gs.print(str.id); // HEre i get 10045
So your final code is:
(function executeRule(current, previous /*null when async*/) {
var body = "{\"fields\":{\"project\":{\"key\":\"CAD\"},\"summary\": " + "\"" + current.short_description + "\",\"issuetype\": {\"name\":\"Story\"},\"description\":{\"type\":\"doc\",\"version\":1,\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":" + "\"" + current.description + "\",\"type\":\"text\"}]}]}}}";
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://xxx.atlassian.net/rest/api/3/issue');
request.setHttpMethod('POST');
request.setRequestBody(body);
var user= 'xxxx';
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.id;
current.u_jira_issueid = targetIncidentID;
current.update();
gs.log("HTTPCODE:"+httpStatus+"BODY"+response.getBody());
gs.log("Jira targetIncidentID: "+targetIncidentID);
})(current, previous);
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2020 05:21 AM
Really appreciated your quick response. It worked like magic.