Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Client script to populate choices variable select Box

Rick27
Tera Contributor

Hello, I created an include script making a GET API call and I would like to get the response and populate a select box variable from a service catalog, but I don't know exactly how to assemble this client script, it would be OnChange:

 

My script Include Ajax

 

var UpgradePreviewAjax = Class.create();
UpgradePreviewAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
populateData: function() {
var patch_instance = this.getParameter('sysparm_patch_instance');
var url = patch_instance;
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + patch_instance + '.service-now.com/api/now/table/upgrade_preview?sysparm_query=status%3Dcomplete&sysparm_fields=target_version%2Csource_version&sysparm_limit=');
request.setHttpMethod('GET');
var user = 'user';
var password = 'pass';
request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestBody(JSON.stringify(requestBody));
return request.execute();
},


type: 'UpgradePreviewAjax'
});

 

2 REPLIES 2

Prateek kumar
Mega Sage

Check this out:

Solved: fetching data from rest API - ServiceNow Community


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Hitoshi Ozawa
Giga Sage
Giga Sage

Something like below. Will add values of field "source_version" to selectbox named "SelectboxName".

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('UpgradePreviewAjax');

    // Add parameters that you want to pass to the Script Include
    ga.addParam('sysparm_name', 'populateData');

    // Make the asynchronous call to the Script Include
    ga.getXMLAnswer(function(response) {
        // Process the response
        var answer = response.responseXML.documentElement.getAttribute('answer');
        var jsonData = JSON.parse(answer);

        // add to selectbox
        jsonData.forEach(function(item) {
            g_form.addOption('SelectboxName', item.source_version);
        });
    });
}