How to get value from Json response body into field

attanhes
Tera Guru

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).

find_real_file.png

But I am getting below errors on the logs.

find_real_file.png

Can someone please help me.

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

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

View solution in original post

3 REPLIES 3

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

var targetIncidentID = obj.HTTPCODE['id'];

 

Try this.

Thanks,
Ashutosh

Ashutosh Munot1
Kilo Patron
Kilo Patron

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

Really appreciated your quick response. It worked like magic.