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

Hi Hitoshi.

I am completely new to servicenow and trying to learn how to do exactly what you are showing. Do i have to create 2 select fields in UI Builder? If so - i managed to do that. I am stuck with attaching the onChange function to the field in UI builder.  May i humbly ask you to guide please.

 

Thank you.