Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Adding choices using addOption?

miro2
Mega Sage

Hi,


I created a table that extends Data Lookup Matcher Rules (I don't use Data lookup rules for this purpose). Now I want to use GlideAjax and a Script Include to look up my custom table. When the HR Service and the HR Case State match a record in that table, I want to add choices to the Sub-State field using addOption().
My goal is to show the Sub-State field with dynamic choices when the HR Case state changes to Work in Progress. The Sub-State field is an integer field and currently has no predefined choices.

miro2_1-1763735127886.png


Is it possible to add options dynamically with addOption and still have the form save the selected option correctly? When I tried this, the new Sub-state labels appeared correctly when the record moved to Work in Progress, but after saving, only the values were stored. It seems like these new choices, value in blue, aren’t being stored.

Choices defined in custom table:

miro2_2-1763739855528.png

 


Custom table records used for matching

miro2_0-1763746439732.png

 

 

Script include

    getSubStates: function() {
        var hrService = this.getParameter('sysparm_hr_service');
        var caseState = this.getParameter('sysparm_case_state');
        var subStates = [];

        var gr = new GlideRecord('my_custom_table');
        gr.addQuery('u_hr_service', hrService);
        gr.addQuery('u_hr_case_state', caseState);
        gr.orderBy('u_sub_state_label');
        gr.query();

        while (gr.next()) {
            var choiceValue = gr.getValue('u_sub_state_value');
            var choiceLabel = gr.getDisplayValue('u_sub_state_label');

            subStates.push({
                value: choiceValue,
                label: choiceLabel
            });
        }

        return JSON.stringify(subStates);
    },


client script on change

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

    var hrService = g_form.getValue('hr_service');
    var caseState = g_form.getValue('state');

    var ga = new GlideAjax('HRCaseSubStateLoader');
    ga.addParam('sysparm_name', 'getSubStates');
    ga.addParam('sysparm_hr_service', hrService);
    ga.addParam('sysparm_case_state', caseState);

    ga.getXML(populateSubStates);

    function populateSubStates(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        g_form.addInfoMessage('Response: ' + answer);

        if (answer) {
            var subStates = JSON.parse(answer);
            
            if (subStates && subStates.length > 0) {
                for (var i = 0; i < subStates.length; i++) {
                    g_form.addInfoMessage('Adding - Value: ' + subStates[i].value + ', Label: ' + subStates[i].label);                    
                    g_form.addOption('u_sub_state', subStates[i].value, subStates[i].label);
                }
            } else {
                g_form.addInfoMessage('No sub-states available for the selected HR Service and Case State.');
            }
        } else {
            g_form.addInfoMessage('No response received from server.');
        }
    }
}

This is how it looks like when state changes to work in progress

demo.gif

Could the issue be caused by the Sub-state field not having any defined choices? and then it can’t be stored this way.

0 REPLIES 0