Pass variable as argument to setStringParameterNoEscape('name', 'value' );

Marc White
Tera Contributor

Trying unsuccessfully to pass a populated variable instead of a string.

This works

r.setStringParameterNoEscape('engine', '192.168.5.5:1671' );

This does not

var target = "192.168.5.5:1671"

r.setStringParameterNoEscape('engine', target );

 

 

6 REPLIES 6

DScroggins
Kilo Sage
Hello, Have you tried using a different variable name besides 'target' like 'targetIP' instead? For example: var targetIP = "192.168.5.5:1671"; r.setStringParameterNoEscape('engine', targetIP );

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Marc,

what value it sets?

Did you try to print the json request body being sent out?

Have you created variable substitution for that under the rest message function?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Marc White
Tera Contributor

Using the variable name 'targetIP' does not work.  Wrapping it in JSON does not work.  Stringify does not work. Escaping it and not escaping it makes no difference - it doesn't work.  It seems to only accept plain text enclosed in single quotes. Period.  

I am not calling it from a form or Client script so there is no current.targetIP. The call is being made in a Script Include javascript.

I am able to test with the System Webservices REST Test feature passing 192.168.5.5:1671 as a Variable Substitution successfully.

find_real_file.png

 

This works -

targetIP = "192.168.5.5:1671"

try {
var r = new sn_ws.RESTMessageV2('x_nexsa_cmdb_pop.NXT Get all Device Scores', 'GetAllScores');
   r.setMIDServer('marcmidserver');
   r.setEccParameter('skip_sensor', true);
   r.setStringParameterNoEscape('engine', '192.168.5.5:1671');      // Replace second argument with targetIP
   r.setStringParameterNoEscape('device', 'CHIA428');
   r.setStringParameterNoEscape('DESquery', '%29%20%28fro0device%20%28where%20device%20%28eq%20');

   var response = r.execute();
   var responseBody = response.getBody();
   var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
} finally {
requestBody = r ? r.getRequestBody():null;

What is the syntax to get targetIP into the second argument ??

Marc,

 

You have the correct syntax for passing a dynamically set variable as the second argument. My suggestion is you should enable HTTP logging for the outbound REST you are calling 'GetAllScores'.

1. Open the HTTP Method record for 'GetAllScores'

2. Under related links click 'Set HTTP Log level'

find_real_file.png

3. In the popup choose 'All'

find_real_file.png

 

Now try executing your rest message with the variable substitution like so:

targetIP = "192.168.5.5:1671"

try { 
var r = new sn_ws.RESTMessageV2('x_nexsa_cmdb_pop.NXT Get all Device Scores', 'GetAllScores');
   r.setMIDServer('marcmidserver');
   r.setEccParameter('skip_sensor', true);
   r.setStringParameterNoEscape('engine', targetIP );
   r.setStringParameterNoEscape('device', 'CHIA428');
   r.setStringParameterNoEscape('DESquery', '%29%20%28fro0device%20%28where%20device%20%28eq%20');

   var response = r.execute();
   var responseBody = response.getBody();
   var httpStatus = response.getStatusCode();
}
catch(ex) { 
var message = ex.message; 
} finally { 
requestBody = r ? r.getRequestBody():null; 
} 

 

After you can navigate to 'Outbound HTTP Requests' under System Logs and view the details of your outbound request (request & Response).

 

Hopefully that will help you determine what error is occurring.