REST - Post from a Workflow Script

stevemkuk
Tera Contributor

Hello,

Just to say this is my first time trying to use/configure a REST message and have no previous knowledge at all !!!!

I am struggling with a REST message using a variable from a catalog request.

I have created a REST Message with an Endpoint that ends in 'request/servicenow/${username}'

The content is 

<servicenow>

<username>${username}</username>

</servicenow>

The variable substitution is username No Escaping

The preview Script is as follows:

try { 
 var r = new sn_ws.RESTMessageV2('Test Steve', 'Test POST');
 r.setStringParameterNoEscape('username', '');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}
If I run a Test from the message then something is received <servicenow> <username></username> </servicenow>
If I manually add a username then this also passed through ok.

I have a catalog item to gather the user name with a variable name of 'username'

There is a workflow with a run script as follows:
try {
var r = new sn_ws.RESTMessageV2('Test Steve', 'Test POST');

r.setStringParameterNoEscape('username', '');

//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
r.setMIDServer('XXXXXXXX-DEV');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
r.setEccParameter('skip_sensor', true);

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

}
catch(ex) {
var message = ex.message;
}


There are no errors but the endpoint only receives '<servicenow> <username></username> </servicenow>'
The username is not being embedded in the message as required.

Can anyone please help??

Thanks

Steve
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are not setting the value in username variable if you are using variable substitution

try {
    var r = new sn_ws.RESTMessageV2('Test Steve', 'Test POST');
    r.setStringParameterNoEscape('username', <set some value here>);
    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
}
catch(ex) {
    var message = ex.message;
}

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are not setting the value in username variable if you are using variable substitution

try {
    var r = new sn_ws.RESTMessageV2('Test Steve', 'Test POST');
    r.setStringParameterNoEscape('username', <set some value here>);
    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
}
catch(ex) {
    var message = ex.message;
}

Regards
Ankur

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

Thanks for this,

I added  r.setStringParameterNoEscape('username', current.variable_pool.username);

and it works great.

Regards

Steve

stevemkuk
Tera Contributor

Hello again,

 

Thanks for your assistance with this and by adding the following line it does indeed send the entered username:

 r.setStringParameterNoEscape('username', current.variable_pool.username);

 

This works great if the username is first.last 

But nothing is sent if the username is entered as first last.

If you separate the names by a space then nothing is sent out by the script.

Are you able to advise why this may be?

 

Thanks

 

Steve