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

Harsh Vardhan
Giga Patron

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. 

 

https://community.servicenow.com/community?id=community_article&sys_id=ad8ad908dbdc7b44a39a0b55ca961...

Tony Chatfield1
Kilo Patron
Hi, I suspect you need to maintain the existing syntax of the string, maybe try something like \'"' myvariable '\"' to encapsulate the text you don't include in your var

Harsh Vardhan
Giga Patron

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);

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>).