Constructing a REST message body using variable values from screen (form)

RajanMurkute
Mega Guru

I'm trying to send a POST request with recordless REST messagev2, that is very similar to the example script below -

var restMessage = new sn_ws.RESTMessageV2();
restMessage.setBasicAuth("abc", "efgh");
restMessage.setHttpMethod("post");
restMessage.setEndpoint("http://<instance>.service-now.com/api/now/table/incident");
restMessage.setRequestBody("{\"short_description\" : \"Test incident\"}");
var response = restMessage.execute();

On the UI screen, I have field 'Short Description' (Name= u_shortDescription).
How do I insert the value of the field 'Short Description' in my script, instead of the string
\"Test incident\"?

I want the target to receive the value on the UI. (ie. current.u_shortDescription).

I tried using following syntax -
var sd = current.u_shortDescription;
restMessage.setRequestBody('{\"short_description\" : \"{{sd}}\" }');
OR
var sd = current.u_shortDescription;
restMessage.setRequestBody('{\"short_description\" : \"$(sd)\" }');
But none of the above works. The POST requests inserts a record on the target table, but the field 'short_description' is left blank.
Can someone please help with the correct syntax?
 


6 REPLIES 6

can you paste your business rule screenshot ?

Tony Chatfield1
Kilo Patron

If you haven't already checked you may also want to confirm the format the destination allows\accepts for each field; Not all integrations are built as well as SNC's and your target may not be consistant with the way it treats different fields.

I sometimes also find it easier visually to create an object then set it as the request body.

var myObject = { 'field_1' : true, 'field_2' : 'Some Text', 'field_3' : dotwalk.myValue };   

myAction.setRequestBody(JSON.stringify(myObject));