Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

g_form.addOption() is not working inside GlideAjax callback function

HoangT551406063
Tera Contributor

I have a MRVS have a field have type is Select Box, in my onLoad client script have a callback function in GlideAjax to get question choice in order to add options to select box field. I tried to log the response and it working correctly. but when I call g_form.addOption() outside the callback function then the option is added correctly but when I tried to call it inside the callback function it is not working. Please help.

//old options is cleared before this line        
var optionAjax = new GlideAjax('scoped_app.script_include');
        optionAjax.addParam('sysparm_name', 'functionname');
        optionAjax.addParam('sysparm_question_id', varId);
        // g_form.addOption('value_select_box', choiceValue, choiceLabel); this line work correctly but the addOption line below is not
        optionAjax.getXMLAnswer(function(response) {
            var res = JSON.parse(response); //[{'option_value':'value', 'option_text':'text'}]
            for (var i = 0; i < res.length; i++) {
                var choiceValue = res[i].option_value+'';
                var choiceLabel = res[i].option_text+'';
                g_form.addOption('value_select_box', choiceValue, choiceLabel);
//I also tried g_form.addOption('value_select_box', choiceValue, choiceLabel, i+1); but still not working
            }
        });
10 REPLIES 10

Hi @HoangT551406063 ,

I noticed that variable is configured as hidden. Could you uncheck that option first so we can rule out any conflict between the Client Script and the visibility settings?

Kind regards,

 

Ankur Bawiskar
Tera Patron

@HoangT551406063 

are you sure the JSON parsing is iterating properly?

did you check the length?

//old options is cleared before this line        
var optionAjax = new GlideAjax('scoped_app.script_include');
optionAjax.addParam('sysparm_name', 'functionname');
optionAjax.addParam('sysparm_question_id', varId);
// g_form.addOption('value_select_box', choiceValue, choiceLabel); this line work correctly but the addOption line below is not
optionAjax.getXMLAnswer(function(response) {
    var res = JSON.parse(response); //[{'option_value':'value', 'option_text':'text'}]
    alert('length' + res.length);
    for (var i = 0; i < res.length; i++) {
        var choiceValue = res[i].option_value + '';
        var choiceLabel = res[i].option_text + '';
        g_form.addOption('value_select_box', choiceValue, choiceLabel);
        //I also tried g_form.addOption('value_select_box', choiceValue, choiceLabel, i+1); but still not working
    }
});

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

i checked the parsed json and the length of the array and there is no error occured, i also check the value of choiceValue and choiceLabel and it's correctly the only incorrect line is the addOption()

@HoangT551406063 

share the variable config screenshots.

that variable should be of type Select box and I hope the choice values you are adding are already there in question_choice for that variable?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,

 

Below is my config screenshot, i dont set any question_choice in for variable because there are too many choices (over 1k choices) so i script to get it from server. I have to add the question_choice, haven't I?

 

Screenshot 2026-04-07 090934.png