Constructing a REST message body using variable values from screen (form)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2019 12:14 AM
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?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2019 12:34 AM
where are you using the script ? in business rule ?
adding one article here, kindly have a look. it has mentioned steps to use post method to create record in target instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2019 12:37 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2019 12:49 AM
you can try something like this. It has tested.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var sh_desc = current.short_description; // fetching your short description
var desc = current.description; // fetching your short description
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":"'+sh_desc+'",' +
'"description":"'+desc+'"}');
var response = restMessage.execute();
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2019 11:15 PM
Thanks, Harshvardhan.
yes, I am using a Business Rule, that runs after the record is inserted in the target table (a non-ServiceNow instance).
But the statement (copied from your script example)-
restMessage.setRequestBody('{"short_description":"'+sh_desc+'",' +
'"description":"'+desc+'"}');
gives error. (It does not let me save the script, stating 'Unclosed string'). Looks like there is some mix-up of double and single quotes.
Also I read the article at the link you sent. That article too, is useful for SN to SN integration. In my case my target instance is not a ServiceNow instance. Therefore I am trying to use RESTmMessageV2(), and not RESTMessageV2(<message name>, <method>).