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

I added the httpStatus log

patricklatella
Mega Sage

but neither attempt created a new record in the target instance...do those logs indicate anything to help troubleshoot?

Naveen Velagapu
Mega Guru

hi patricklatella, 

If i understood correctly, you are able to connect to target instance and request object is getting generated. 

1. Did you verified the web service in the target instance, is it sending a proper response? 

2. Can you keep some logs in the target instance web service ?  

3. Are you setting the variable substitutions to expected values?

4. Are you using table API or Custom Web service in Target ?

 

Hi naveenvelagapudi, I'm unfortunately a little new at REST integrations...the logs I showed were all the debugging I'm done so far.  How would I verify the elements you asked?  Thanks 

patricklatella
Mega Sage

also, this was working recently (another developer built it, then did a couple updates and broke it, and now I'm struggling to fix it).

So I know the RESTmessage is working