Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to pass the value dynamically in REST messages endpoint

vinuth v
Tera Expert

vinuthv_0-1665576798498.png

instated of {topicName} I wanted to pass the value for example a1_topic. And using put method I need to pass the payload.

I tried with the below script 

 

var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {
'grant_type': 'client_credentials',
'client_id': 'MLW2',
'client_secret': 'gAA'
};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('EDL ', text);
var token1 = tokenResponse.getToken();
var token = token1.getAccessToken();
// var tokenParsed = JSON.parse(token);
// var token2 = tokenParsed.token;
gs.print("update existing token" + token);
var topicName="a15_topic";
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setStringParameterNoEscape("Token", token);
//restMessage.setAuthenticationProfile('oauth2', '43c1772e1b8ad9502fdb844467777');
restMessage.setHttpMethod("put");
restMessage.setEndpoint("https://api-dev.nametopic.com/data/v1/topics/{topicName}");
restMessage.setRequestBody("request_id","RITM1");
restMessage.setRequestBody("number","2");
var response = restMessage.execute();
gs.print("test" +response);

 

 

I am getting like this error 

escaped absolute path not valid: java.lang.IllegalArgumentException: Invalid uri

Please any one look into this one.
 

1 REPLY 1

Sebastian L
Mega Sage

So I would add it in your HTTP Methods, and create one with corresponding with your action. I would honestly also have all your OAUTH done by an OAUTH profile also instead of all that custom scripting... 

 

In your endport you write "https://api-dev.nametopic.com/data/v1/topics/${topicName}" 

And under Variable substitution you add one with name "topicName" and no escaping.

so ${topicName} is what is being replaced.  Se example for an azure integration using these principles. 

 

Then in your script you don't need to provide the URL, instead you write: 

 

var r = new sn_ws.RESTMessageV2('name_of_rest_message, 'name_of_HTTP_METHOD'); 
r.setStringParameterNoEscape('topicName', topicName);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

 

For my Azure example I would write: 

var group_id = 'x';
var r = new sn_ws.RESTMessageV2('Azure AD', 'Add Members to Group');
r.setStringParameterNoEscape('group-id', group_id);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

Best regards,
Sebastian Laursen