Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help with REST Script

patricklatella
Mega Sage

Hi all,

I'm working on a one-way REST integration between 2 instances.  This script is running in a business rule on the source instance, and is supposed to create a new record on the target instance.  It was working prior to some updates and now is not working...anyone see what I'm missing?  

This is triggered by a record producer on the source instance.

For now I'm just trying to create the record on the target incident table and populate the 'description' field.

Here's the script with a couple log entries"

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

function jsonEncode(str){
str = new JSON().encode(str);
return str.substring(1, str.length - 1);
}
if(current.contact_type == 'self-service') {//set correctly in the record producer script
try {
var r = new sn_ws.RESTMessageV2('One Way OB', 'POST');
r.setStringParameterNoEscape('description', jsonEncode(current.description + ''));
gs.info('desc is '+current.description); //log correctly shows the value in the description field on the form 
var response = r.execute();
gs.info('response is '+response); //log shows "response is [object RESTResponseV2]"
var responseBody = response.getBody();
gs.info('responseBody is '+responseBody); //log shows "responseBody is {"error":{"message":"Exception while reading request","detail":"Cannot decode: java.io.StringReader@5d5403"},"status":"failure"}" error has something to do with this?
var httpStatus = response.getStatusCode();
var jsonObject = JSON.parse(responseBody);
}
catch(ex) {
var message = ex.getMessage();
}
}
})(current, previous);

1 ACCEPTED SOLUTION

patricklatella
Mega Sage

we got it working, here's our script:

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


function jsonEncode(str)
{
str = new JSON().encode(str);
return str.substring(1, str.length - 1);
}


try {
var r = new sn_ws.RESTMessageV2('One Way OB', 'Post');

r.setStringParameterNoEscape('caller_id', current.caller_id.name);

r.setStringParameterNoEscape('requested_by_caller', current.u_requested_by_caller.name);

r.setStringParameterNoEscape('impact', current.impact);

r.setStringParameterNoEscape('urgency', current.urgency);

r.setStringParameterNoEscape('short_description', current.short_description);

r.setStringParameterNoEscape('description', jsonEncode(current.description + ''));

r.setStringParameterNoEscape('is_service_request', current.u_is_service_request);

r.setStringParameterNoEscape('practice', current.u_what_technology_is_this_related_to.u_name);

r.setStringParameterNoEscape('work_engagement', current.u_what_work_engagement_is_this_related_to.u_name);

r.setStringParameterNoEscape('required_completion_time', current.required_completion_date_time);

r.setStringParameterNoEscape('required_start_time', current.required_start_date_time);
//gs.log("working");
var response = r.execute();
var responseBody = response.getBody();
//gs.log("Body is :" +responseBody);
var httpStatus = response.getStatusCode();
//gs.log("httpsstatus code:" + httpStatus);
var jsonObject = JSON.parse(responseBody);
}
catch(ex) {
var message = ex.getMessage();
}



})(current, previous);

View solution in original post

12 REPLIES 12

Hardik Benani
Mega Sage
Mega Sage

Can you try removing replacing  

var responseBody = response.getBody(); with 

var responseBody =JSON.parse(response.getBody());

 

Second thing to try is:

Replace

r.setStringParameterNoEscape('description', jsonEncode(current.description + '')); with

r.setStringParameter('description', current.description + ''); 

Hi Hardik, 

thanks for the response:  the first script logged this:

find_real_file.png

trying the 2nd one now

so for the 2nd try, I kept the first as well...was that how I should have tested it?  

this was the response when I did both of your replacements

find_real_file.png