Setting dynamic end point for post in REST message

harry24
Tera Contributor

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 

2 REPLIES 2

Pankaj Jadhav
Tera Contributor

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.

Runjay Patel
Giga Sage

Hi @harry24 ,

 

Follow below steps to create end point.

 

  1. Create one REST Message with POST Method. Set authentication method and End point provided by third-party.
  2. RunjayPatel_0-1731744757826.png

     

  3. Create post method.
  4. RunjayPatel_1-1731744837437.png

     

  5. In query parameter add your variable as many as you want, i have added 2 in this example.
  6. RunjayPatel_2-1731744940014.png

     

  7. Click related link "Auto generate variable", it will generate variable for you in related list.
  8. RunjayPatel_3-1731745069091.png

     

  9. Click on related link "Preview Script Usage" to generate the script.
  10. RunjayPatel_4-1731745229630.png

     

 

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

-------------------------------------------------------------------------