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

@appstorm Here is the revised code from my side which should work.

//Client script

 

 

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){
        console.log("TESTING " + response);
        var res = JSON.parse(response);
        console.log("TESTING " + res.mobile_phone.toString());
        
        g_form.setValue('u_test_string',res.mobile_phone.toString());
    }
    
}

 

 

Here is the script include.

 

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDetails: function(){
        gs.addInfoMessage('script include triggered');
        
        var userId = this.getParameter('userId');
            gs.addInfoMessage('user scr--'+userId);
        var obj = {};

    var grSysUser = new GlideRecord('sys_user');
    if (grSysUser.get(userId)) {
        obj.mobile_phone =  grSysUser.getValue('mobile_phone');
        obj.email =  grSysUser.getValue('email');
        // obj.user_id = grSysUser.getValue('user_id');
    }
    gs.addInfoMessage('obj '+JSON.stringify(obj));
    return JSON.stringify(obj);
    },

    type: 'sampleUtils'
});

 

Hope this helps.

 

appstorm
Tera Contributor

Thank you!  I am getting a "Unhandled exception in GlideAjax" for the CS.  I have a reference field (u_user) that auto-loads the ID for the logged-in user.  In theory, the select box field (mobile_phone) should then display data on page-load, correct?

Ideally, yes. Could you please share the CS and SI code once again so that I can check the possible source of the error.

SI:

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDetails: function(){
        gs.addInfoMessage('script include triggered');
        
        var userId = this.getParameter('userId');
            gs.addInfoMessage('user scr--'+userId);
        var obj = {};

    var grSysUser = new GlideRecord('sys_user');
    if (grSysUser.get(userId)) {
        obj.mobile_phone =  grSysUser.getValue('mobile_phone');
        obj.email =  grSysUser.getValue('email');
        // obj.user_id = grSysUser.getValue('user_id');
    }
    gs.addInfoMessage('obj '+JSON.stringify(obj));
    return JSON.stringify(obj);
    },

    type: 'sampleUtils'
});

 

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){
        console.log("TESTING " + response);
        var res = JSON.parse(response);
        console.log("TESTING " + res.mobile_phone.toString());
        
        g_form.setValue('u_test_string',res.mobile_phone.toString());
    }
    
}

 

@appstorm Can you try the following and see if it works.

 

SI:

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDetails: function(){
        gs.addInfoMessage('script include triggered');
        
        var userId = this.getParameter('sysparm_userId');
            gs.addInfoMessage('user scr--'+userId);
        var obj = {};

    var grSysUser = new GlideRecord('sys_user');
    if (grSysUser.get(userId)) {
        obj.mobile_phone =  grSysUser.getValue('mobile_phone');
        obj.email =  grSysUser.getValue('email');
        // obj.user_id = grSysUser.getValue('user_id');
    }
    gs.addInfoMessage('obj '+JSON.stringify(obj));
    return JSON.stringify(obj);
    },

    type: 'sampleUtils'
});

 

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('sysparm_userId',user); //Parameters
    ga.getXMLAnswer(getResponse);
    
    function getResponse(response){
        console.log("TESTING " + response);
        var res = JSON.parse(response);
        console.log("TESTING " + res.mobile_phone.toString());
        
        g_form.setValue('u_test_string',res.mobile_phone.toString());
    }
    
}

 

Hope this works.