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.

How to pass variables dynamically in REST Message Body?

Deepak K
Mega Guru

I am trying to create an incident in another ServiceNow instance using Rest Integration with Table API. and I am using Business Rule which will run after Incident created.

Incident is getting created in the target ServiceNow instance but it was not passing any values.

Here is the code that I have tried.

 

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

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://instance.service-now.com/api/now/table/incident?sysparm_display_value=all&sysparm_fields=number%2Cstate%2Cpriority%2Cshort_description');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin123';

var body = {
"caller_id":current.caller_id,
"priority" : current.priority,
"short_description":current.short_description
};
var requestBody = JSON.stringify(body);

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(requestBody);

var response = request.execute();
gs.log(response.getBody());
gs.addInfoMessage(response.getBody());

})(current, previous);

 

Could you guys help me to sort this out?

Thanks

1 ACCEPTED SOLUTION

Dan Ellis
Kilo Sage

Hi Deepak,

If you log the "requestBody" variable does it contain all the values?

gs.info("requestBody: " + requestBody);

Also, as you are dot-walking it uses a reference to the object, so you could try using "current.getValue('variable_name');"

Let me know how you get on.

Thanks,
Dan

View solution in original post

2 REPLIES 2

Dan Ellis
Kilo Sage

Hi Deepak,

If you log the "requestBody" variable does it contain all the values?

gs.info("requestBody: " + requestBody);

Also, as you are dot-walking it uses a reference to the object, so you could try using "current.getValue('variable_name');"

Let me know how you get on.

Thanks,
Dan

Thanks Dan. 🙂

It is working when I set the values using getValue();