Populate Servicenow Drop down list in real time from external cloud data source (using API?)

mattmm
Kilo Sage

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?

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

find_real_file.png

find_real_file.png

View solution in original post

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

find_real_file.png

find_real_file.png

Thanks @Hitoshi Ozawa ! I have seen some other articles that are using Ajax to do this. Would using Ajax mitigate the issues with slowness or data loss as per the below?

https://community.servicenow.com/community?id=community_question&sys_id=b48e4b21dbdcdbc01dcaf3231f961958

 

https://community.servicenow.com/community?id=community_question&sys_id=d54c8b65db9cdbc01dcaf3231f961946

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.