add the select box value dynamically

VaibhavJ
Tera Contributor

Hi All,

 

I am trying to add the value dynamically in the select box for one of the group of users by using below catalog client script...

 

function onLoad() {
    var selectBox = g_form.getControl('what_is_this_request_for');
    var Compuser = new GlideAjax('InteractionUtils');
    Compuser.addParam('sysparm_name', 'isUserInCorporate_Compensation_Group');
    Compuser.getXML(function(response) {
         var isInGroup = response.responseXML.documentElement.getAttribute('answer');
        selectBox.options.length = 0;
        if (isInGroup == 'true') {
            selectBox.addOption('option_value_1', 'Job Codes');
            selectBox.addOption('option_value_2', 'Job Families');
            selectBox.addOption('option_value_3', 'Job Functions');
            selectBox.addOption('option_value_1', 'License or Certification');
            selectBox.addOption('option_value_2', 'Earnings Codes');
            selectBox.addOption('option_value_3', 'Deduction Codes');
            selectBox.addOption('option_value_3', 'Tax Codes');
        } else {
            selectBox.addOption('option_value_1', 'License or Certification');
            selectBox.addOption('option_value_2', 'Earnings Codes');
            selectBox.addOption('option_value_3', 'Deduction Codes');
            selectBox.addOption('option_value_3', 'Tax Codes');

        }
   
});
}
 
I have return function in script include to check the members in the group...
 
isUserInCorporate_Compensation_Group: function(){
        var Member = 'false';
        var compensationGroup = gs.getProperty('corporate_compensation');
        if (gs.getUser().isMemberOf(compensationGroup)) {
            Member = 'true';
        }
        return Member;
    },
 
It is not working, need help on this am i missing anything here?
 
1 REPLY 1

Runjay Patel
Giga Sage

Hi @VaibhavJ ,

 

Try using below code.

function onLoad() {
    var selectBox = g_form.getControl('what_is_this_request_for');
    if (selectBox) {
        // Clear existing options
        selectBox.options.length = 0;

        // Call GlideAjax to check if the user is in the group
        var Compuser = new GlideAjax('InteractionUtils');
        Compuser.addParam('sysparm_name', 'isUserInCorporate_Compensation_Group');
        Compuser.getXML(function(response) {
            var isInGroup = response.responseXML.documentElement.getAttribute('answer');

            // Define options based on group membership
            var groupOptions = [
                { value: 'job_codes', label: 'Job Codes' },
                { value: 'job_families', label: 'Job Families' },
                { value: 'job_functions', label: 'Job Functions' }
            ];

            var defaultOptions = [
                { value: 'license_certification', label: 'License or Certification' },
                { value: 'earnings_codes', label: 'Earnings Codes' },
                { value: 'deduction_codes', label: 'Deduction Codes' },
                { value: 'tax_codes', label: 'Tax Codes' }
            ];

            // Add options to the select box
            var optionsToAdd = isInGroup === 'true' ? groupOptions.concat(defaultOptions) : defaultOptions;

            optionsToAdd.forEach(function(option) {
                selectBox.add(new Option(option.label, option.value));
            });
        });
    }
}

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------