how to send values to rest message through BR

Servicenow10
Kilo Guru

in reference to link: https://community.servicenow.com/community?id=community_question&sys_id=5ab3cd28dbafc4d0feb1a851ca96...

please refer as am stuck with:

 

How are you determining whether the rest call is working fine or not? it works when i just trigger the script 

Are you not getting desired response by sending those variable values?no ,because for two reasons:

case 1 : when you fetch the field value you use syntax:

r.setStringParameterNoEscape('var1', current.sys_id);

case 2: when you fetch values from the variables:

r.setStringParameterNoEscape('var1', current.variables.sys_id);

 

and in my case am not using any of the above method  as you can see in my script that am trying to get values which i have fetched in that script only like i took the value of short description field in description field.

var description = current.short_description;

then i bifurcated that description field in two variables:

var variable1 = "test"

var variable2 = "test"

now i want to send the values of variable1 ,variable2 to my rest msg but how

 

really urgernt!!

1 ACCEPTED SOLUTION

Hi,

As checked, the rest API has to be called from the mid server. Set the midserver in your rest api under http request and then for substitution, try setQueryParameter and check once.

Also, you can call the end point on the fly like this instead of creating the message.

var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("your_API_URL_with_args");
sm.setHttpMethod("get");
sm.execute();

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

7 REPLIES 7

Jorn van Beek
Tera Guru

how does your outgoing rest look like? are you using the web services or using integration hub? please share the full script... which error messages do you get, how does the outgoing JSON looks like? In short help us to help you

hi jorn,

how does your outgoing rest look like? am using a web service

please share the full script?

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

// Add your code here
var description = current.short_description;
var index = description.indexOf("Some String");
index = index + 19;
var variable1= description.substring(15, description.indexOf("Some String"));
gs.log("value 1 is " + variable1);

var variable2= description.substring(index, description.indexOf("Some String"));
gs.log("value 2 is " + variable2);

var r = new sn_ws.RESTMessageV2('RestMessage', 'Default GET');

//how to send the values of variable1,variable2 to rest message
r.setStringParameterNoEscape('endpointvariable1', variable1); //variable1

r.setStringParameterNoEscape('endpointvariable1', variable2); //variable2

 

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

})(current, previous);

 

which error messages do you get, how does the outgoing JSON looks like? am getting 0 error code

in rest message when i tried to trigger some other ps script then it works the only thing is this:

r.setStringParameterNoEscape('endpointvariable1', variable1); //variable1

r.setStringParameterNoEscape('endpointvariable1', variable2); //variable2

 and why its not working i have mentioned in my question.

 

if you any doubts please let me know i really have to resolve it asap

thanks!!

 

Could you show the body of the rest message you try to send?

 

I have seen the 0 error when the endpoint I was consuming did not process the request in time. This could be due to not understanding the incoming message in combination with missing error handleing.

hi jorn,

 

rest message works why am saying this because when i triggere the rets message with some other script it does work and shows the o/p but when am trying to send parameter it doesn't take it dynamically....like as i said in the code you can see that the :

r.setStringParameterNoEscape('endpointvariable1', variable1); //variable1

r.setStringParameterNoEscape('endpointvariable1', variable2); //variable2

so when I change the above code as :

r.setStringParameterNoEscape('endpointvariable1', 'variable1'); //variable1

r.setStringParameterNoEscape('endpointvariable1', 'variable2'); //variable2

it works