Setting dynamic end point for post in REST message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 05:25 AM
Hello ,
I have a requirement where I need to dynamically create end-points for POST .
Like whenever a Change is created in servicenow at build stage the integration invokes and creates the change in third party .
https://dev.abc.com/rest/v1/projects/ThirdPartyIntegration/workitems
this works fine
Now the project name will be dynamic how do i create this ?
Also how can be handle multiple endpoints ?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2024 11:55 PM
Hi Harry,
You can use the string manipulation.
e.g.
//store the project name in variable from function parameter or through gs.getPropety("project_name");
var projectName = "ThirdPartyIntegration";
var url = "https://dev.abc.com/rest/v1/projects/"+ projectName +"/workitems";
This way Project name can be inserted dynamically.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2024 12:22 AM
Hi @harry24 ,
Follow below steps to create end point.
- Create one REST Message with POST Method. Set authentication method and End point provided by third-party.
- Create post method.
- In query parameter add your variable as many as you want, i have added 2 in this example.
- Click related link "Auto generate variable", it will generate variable for you in related list.
- Click on related link "Preview Script Usage" to generate the script.
try {
var r = new sn_ws.RESTMessageV2('test', 'Create change');
r.setStringParameterNoEscape('number', 'CHG0030001');
r.setStringParameterNoEscape('project_name', 'ApplicationServerPeopleSoft');
//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;
}
Note: Fill all the content type and authentication method properly with correct information and then you can test this method while clicking on related link "test".
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------