
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2022 07:37 PM
Can ServiceNow call an API to populate a drop-down list of locations in real-time from an external Azure hosted datasource holding location data? If so, what are the advantages/disadvantages of this?
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 02:29 AM
Here's a sample script.
Create a Service Catalog with 2 fields of Select box. The first field named "field" contains values "common" and "official". The second field will contain values obtained from REST API call.
onChange Client Script on field "field".
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var url = 'https://restcountries.com/v3.1/all';
try {
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.setRequestHeader("Content-type", "application/json");
request.send();
request.onload = function() {
//var data = this.response;
var json = JSON.parse(this.response);
g_form.clearOptions('value');
for (var i = 0; i < json.length; i++) {
var v = json[i].name[newValue];
g_form.addOption('value', v, v);
}
};
} catch (e) {
alert(e.message);
}
}
Execution results. It takes about 2 seconds to fill the second field with values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2022 11:31 PM
Hi Matt,
It is possible to dynamically populate a Select Box (Choice) from external REST API. The disadvantage is that API call may be slow or if the API times out, not all values may be fetched to populate the selection.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 02:29 AM
Here's a sample script.
Create a Service Catalog with 2 fields of Select box. The first field named "field" contains values "common" and "official". The second field will contain values obtained from REST API call.
onChange Client Script on field "field".
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var url = 'https://restcountries.com/v3.1/all';
try {
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.setRequestHeader("Content-type", "application/json");
request.send();
request.onload = function() {
//var data = this.response;
var json = JSON.parse(this.response);
g_form.clearOptions('value');
for (var i = 0; i < json.length; i++) {
var v = json[i].name[newValue];
g_form.addOption('value', v, v);
}
};
} catch (e) {
alert(e.message);
}
}
Execution results. It takes about 2 seconds to fill the second field with values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 04:26 PM
Thanks
https://community.servicenow.com/community?id=community_question&sys_id=b48e4b21dbdcdbc01dcaf3231f961958
https://community.servicenow.com/community?id=community_question&sys_id=d54c8b65db9cdbc01dcaf3231f961946

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 05:27 PM
Hi,
The thread seems to suggest calling REST API from ServiceNow's server. The script I've provided is calling REST API from client web browser.
Performance is going to depend on whether it is faster to call REST API from ServiceNow site or from end-user's pc. Calling via ServiceNow does have an overhead of calling ServiceNow to make the call and then getting the result back from ServiceNow server. The other down side with server-side script is that if there are many users, ServiceNow's instance response time may decrease. ServiceNow will queue the requests to make the REST call. If there are many REST calls, the queue may get longer.
If the end-user has a very slow internet access and the REST API is returning many other unnecessary fields, it may be faster to let ServiceNow make the call and trim the result on the server-side and only return the necessary fields to end-user.
Another topic to consider is if there is a firewall restricting REST API call to only particular ip address. If there is one, end-user pc may not be able to access the API. This may be a case if Azure and ServiceNow is setup to be only accessible from intranet through vpn.
On the other hand, site such as https://reqres.in/ has a firewall restricting access from ServiceNow. In such a case, server-side script will get an authentication error. In all, it's going to depend on how Azure's instance access and ServiceNow's instance access security is setup.