Populate a Drop-Down from Catalog Client Script

appstorm
Tera Contributor

I am aware of a way to populate a single-line text variable using a catalog client script and SI.  However, is there a way to populate a drop-down choice dynamically using the same method?  We are wanting to eventually bring in data using an API and populate a drop-down on the fly with a list of courses, when a user logs-in.  Since a drop-down uses a list of multiple choices manually entered when the variable is created, I wasn't sure if this data could be populated dynamically or not.

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@appstorm You can build the choices for your drop-down/select box on the fly using an onLoad Client script and GlideAjax call to a server side script include. You can use the following code to add Option in the client script.

g_form.addOption('priority', '6', '6 - Really Low');

 

Here is the link to official documentation https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/client/c_GlideFormAPI#r_GF-AddO...

 

View solution in original post

10 REPLIES 10

Will try, thank you!  In the meantime, I made some changes to the script to remove the GlideAjax error.

 

CS:

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

    // var user = g_form.getValue('u_user');
    var user = g_user.userID;
    //Call script include
    var ga = new GlideAjax('global.sampleUtils'); //Scriptinclude
    ga.addParam('sysparm_name', 'getUserDetails'); //Method
    ga.addParam('userId', user); //Parameters
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
        for (var i = 0; i < answer.length; i++) {
            g_form.addOption('mobile_phone', answer[i].mobile_phone, answer[i].email);
        }
	}
}
}

 

SI:

sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  
  getFilePathChoices : function(){
    var optionsArray = [];
    var processer = new JSON();
    var usr = new GlideRecord('sys_user');
    usr.addQuery('user', this.getParameter('userID'));
    usr.query();
    while(usr.next()){
      optionsArray.push({
        'mobile_phone' : usr.mobile_phone.toString(),
        'email' : usr.email.toString()
      });
    }
  return processer.encode(optionsArray);
  },

    type: 'sampleUtils'
});

 But still unable to populate the drop-down with email and mobile phone.