Get sys_id from Outbound Rest Response

Doug29
Tera Contributor

Hello I am building an integration with a 3rd party to ebound incidents from  our ServiceNow into their ServiceNow. I am testing in my own instances to ensure I understand how this works as this is a new item to learn for me. I have the Rest and Business rule setup and working and I see the sys_id in the Response Body but I cannot figure out the script in the business rule to set the Correlation ID as the remote recode sys_id

 

my code in business rule:

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

try {
var r = new sn_ws.RESTMessageV2('eBond_PODSTest', 'Incident_Post');
r.setStringParameterNoEscape('caller_id.email', current.caller.email);
r.setStringParameterNoEscape('cmdb_ci', current.cmdb_ci);
r.setStringParameterNoEscape('assignment_group', current.assignment_group);
r.setStringParameterNoEscape('description', current.description);
r.setStringParameterNoEscape('number', current.number);
r.setStringParameterNoEscape('close_notes', current.close_notes);
r.setStringParameterNoEscape('priority', current.priority);
r.setStringParameterNoEscape('sys_id', current.sys_id);
r.setStringParameterNoEscape('category', current.category);
r.setStringParameterNoEscape('work_notes', current.work_notes);
r.setStringParameterNoEscape('state', current.state);
r.setStringParameterNoEscape('caller_id', current.caller_id);
r.setStringParameterNoEscape('subcategory', current.subcategory);
r.setStringParameterNoEscape('short_description', current.short_description);

 

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.addInfoMessage('Response Body: ' +responseBody);
gs.addInfoMessage('http:Status: ' + httpStatus);

var responsestring=response.getBody();
var jsonObj=JSON.parse(responseBody);
current.correlation_id=responseBody.sys_target_sys_id.value;

}
catch(ex) {
var message = ex.message;
}
})(current, previous);

 

Screen shot of an example response body, the 'value' is the data I need to populate. 

 

Doug29_0-1681438091648.png

 

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage
Tera Sage

@Doug29 ,

 

As per the shared screenshot, it seems that response body has been truncated, Please ensure that response body has the complete data otherwise it will break the code if we perform some operations on it

 

var response = r.execute();
var responseBody = response.getBody();
var jsonObj = JSON.parse(responseBody);

current.correlation_id jsonObj.result.sys_target_sys_id.value;
current.update();

 

If my answer solved your issue, please mark my answer as āœ…Correct & šŸ‘Helpful based on the Impact.

View solution in original post

2 REPLIES 2

Prince Arora
Tera Sage
Tera Sage

@Doug29 ,

 

As per the shared screenshot, it seems that response body has been truncated, Please ensure that response body has the complete data otherwise it will break the code if we perform some operations on it

 

var response = r.execute();
var responseBody = response.getBody();
var jsonObj = JSON.parse(responseBody);

current.correlation_id jsonObj.result.sys_target_sys_id.value;
current.update();

 

If my answer solved your issue, please mark my answer as āœ…Correct & šŸ‘Helpful based on the Impact.

This worked perfectly thank you so much!