I need to add dynamic url of the customer instances in business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 08:35 AM
Hi,
I am using below script for Incident rest Api in the business rule. The script is working but I need to set the url dynamically as we have so many Servicenow client instances where we need to create a incident.
Please anyone help me how I can set the endpoint of the client instance url dynamically.
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://xxxx.service-now.com/api/now/table/incident');
request.setHttpMethod('POST');
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'name';
var password = 'pswd';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"short_description\":\"Hi\",\"assigned_to\":\"B\"}");
var response = request.execute();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 09:02 PM
Hi
This code is in my developer instance but I need the url of the client instance. How will i get client url to developer instance. can you please suggest.
Thanks,
Revathi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 10:37 PM
Usually, we create and connect the dev environment of one system with the dev environment of another system and test with test and prod with prod. So, you can create a single property
E.g you can create a sys_property with name customer_instance_url. Now in the all the instances you can have separate value based on which system it is going to connect.
Let say company name is ABC and it has three instances ABC-Dev, ABC-Test and ABC-prod
so in your dev environment you can store ABC-Dev as a value of customer_instance_url property and in test ABC-Test as a value of customer_instance_url etc.
Now you can access this in your script like this.
var instanceURL = gs.getProperty('customer_instance_url'); // dynamically gets the instance base url
var endPoint_URL = instanceURL+ 'api/now/table/incident';
Right now as you are connecting with your PDI so you don't have to worry about multiple instance URLs. you can just create a property and story the single target url of your customer.
See screenshot for System property configuration:
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 09:46 PM
Hello Revathi,
Since you are trying to test this script in your personal instance and getting the data from your customer instance then you have to change the URL each time when you try to test this script with different customer instance. You cannot get the dynamic URL of different instance of the customer.
But you can create a mapping between your companies instances and Customers Instances:
For example your company have cpdev, cpqa, cpuat and cp instance
and your customer have ctdev, ctqa, ctuat and ct instance the you can create a property to store the mapping between your company instance name and your customer instance name.
Suppose you know that cpdev instance will be always connected to customer's ctdev instance
and cpqa instance will be always connected to customer's ctqa instance
and cp instance will be always connected to customer's ct instance
you can store the mapping in variable or System property as shown below:
{"cpdev": "ctdev", "cpqa": "ctqa", "cp": "ct"}
in your script you can first get the your company's instance name and then get the customer instance name as shown below:
var nameMapping = {"cpdev": "ctdev", "cpqa": "ctqa", "cp": "ct"};
var instanceName = gs.getProperty('instance_name');
var customerInstance = nameMapping[instanceName];
if (customerInstance) {
var endPoint_URL = "https://" + customerInstance+ '.service-now.com/api/now/table/incident';
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(endPoint_URL);
request.setHttpMethod('POST');
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'name';
var password = 'pswd';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"short_description\":\"Hi\",\"assigned_to\":\"B\"}");
var response = request.execute();
}
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2022 06:10 AM
Hello Revathi,
I believe that I have answered your question. If you think your question is answered then please do close this thread/question by marking the appropriate response as correct.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 11:08 PM
Hi
Please follow below it will helps you.
1. Busines rule script:
Just replace with your source instance names and create system properties to store endpoints of target instances.
(function executeRule(current, previous /*null when async*/ ) {
try {
var instanceName = gs.getProperty('instance_name');
var EndPoint = '';
if (instanceName == 'provideinstancename') { //Dev ex: dev63179
baseEndPoint = gs.getProperty('create sysproperty and store endpoint of client dev give property name'); //ex: https://acompanydev.service-now.com/api/now/table/incident');
} else if (instanceName == 'provideinstancename') { //Preprod
EndPoint = gs.getProperty('create sysproperty and store endpoint of client preprod give property name'); //ex: https://acompanypreprod.service-now.com/api/now/table/incident');
} else if (instanceName == 'provideinstancename') { //Prod
EndPoint = gs.getProperty('create sysproperty and store endpoint of client prod and give property name'); //ex: https://acompany.service-now.com/api/now/table/incident');
}
var r = new sn_ws.RESTMessageV2('rest message name', 'post'); //give rest message name and HTTP method name
r.setStringParameterNoEscape('endpoint', EndPoint); // this base_endpoint you need to define in post message
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'name'; //no need to give user name and passwords here you can cretae system property and save there and call that property as did for endpoints
var password = 'pswd';
request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody("{\"short_description\":\"Hi\",\"assigned_to\":\"B\"}");
var response = request.execute();
} catch (ex) {
var message = ex.message;
}
}
)(current, previous);
2. Open Rest message and give full URL on endpoint as shown below.
3. Below HTTP method name (post) need to provide in this line var r = new sn_ws.RESTMessageV2('rest message name', 'post'); and rest message give as shown above.
Hope you it helps you or let us know if you have anything.
Please Mark ✅ Correct/helpful if applicable, Thanks!!
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar