g_form.addOption() not rendering choices on field switch — ServiceNow Client Script Issue

harshal001
Tera Contributor

Problem Statement

I have a dependent dropdown where selecting an Operations Category dynamically loads filtered Fault Category choices via GlideAjax and g_form.addOption().

Issue: When I select an Operations Category for the first time, choices load correctly. But when I switch to a different Operations Category, the Fault Category dropdown shows blank/-- None -- without a page reload.

Script Include:-

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

getFaultCategoriesByOpsCategory: function() {
var opsCategory = this.getParameter('sysparm_ops_category');

var categoryMapping = {
'fault': [
{ label: 'Hardware Replacement', value: 'hardware_replacement' },
{ label: 'Software Fault Remediation', value: 'Software Fault Remediation' },
{ label: 'Non Hardware Fault - Troubleshooting', value: 'troubleshooting' }
],
'service_request': [
{ label: 'Call Management - Hardware Replacement', value: 'call_management_Hardware_Replacement' },
{ label: 'Call Management - Software Remediation', value: 'call_management_Others' },
{ label: 'Call Management - UPS Hardware Replacement', value: 'call_management_UPS_Hardware_Replacement' },
{ label: 'Call Management - Fans & Cable Replacement', value: 'call_management_Fans_Cable_Replacement' },
{ label: 'Call Management - UPS', value: 'call_management_ups' },
{ label: 'Call Management - Service Request', value: 'call_management_Service_Request' }
]
// ... more mappings
};

var result = categoryMapping[opsCategory] || [];
return JSON.stringify(result);
},

type: 'NCS_FaultCategoryAjax'
});

Client Script:- onChange when u_operations_category

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading)
return;

var faultCategoryField = 'u_fault_category';
g_form.clearOptions(faultCategoryField);

if (newValue == "" || newValue == null)
return;

var ga = new GlideAjax('NCS_FaultCategoryAjax');
ga.addParam('sysparm_name', 'getFaultCategoriesByOpsCategory');
ga.addParam('sysparm_ops_category', newValue);

ga.getXMLAnswer(function(response) {
if (!response)
return;

var choices = JSON.parse(response);
g_form.clearOptions(faultCategoryField);

for (var i = 0; i < choices.length; i++) {
g_form.addOption(
faultCategoryField,
choices[i].value,
choices[i].label
);
}
});
}

I am attaching screen shot of mapping category, please refer that.

harshal001_0-1781677267364.png

 

 

please provide you guidance. Thank you 
#client-script # Servicenow  #glideajax


2 ACCEPTED SOLUTIONS

Hey @harshal001 

Script Include (Client Callable)

var NCS_FaultCategoryAjax = Class.create();
NCS_FaultCategoryAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   getFaultCategoriesByOpsCategory: function() {
       var opsCategory = this.getParameter('sysparm_ops_category');
       var categoryMapping = {
           fault: [
               {
                   label: 'Hardware Replacement',
                   value: 'hardware_replacement'
               },

               {

                   label: 'Software Fault Remediation',
                   value: 'software_fault_remediation'

               },

               {

                   label: 'Non Hardware Fault - Troubleshooting',

                   value: 'troubleshooting'

               },

               {

                   label: 'Call Management - Hardware Replacement',

                   value: 'call_management_hardware_replacement'

               },

               {

                   label: 'Call Management - Software Remediation',

                   value: 'call_management_software_remediation'

               },

               {

                   label: 'Call Management - UPS Hardware Replacement',

                   value: 'call_management_ups_hardware_replacement'

               }

           ],



           service_request: [

               {

                   label: 'Call Management - UPS',

                   value: 'call_management_ups'

               },

               {

                   label: 'Call Management - Service Request',

                   value: 'call_management_service_request'

               },

               {

                   label: 'Standby Request',

                   value: 'standby_request'

               },

               {

                   label: 'Vulnerability Check Only',

                   value: 'vulnerability_check_only'

               },

               {

                   label: 'Preventive Maintenance Service',

                   value: 'preventive_maintenance_service'

               },

               {

                   label: 'Upgrade/Patch Services',

                   value: 'upgrade_patch_services'

               },

               {

                   label: 'License or Software Download Request',

                   value: 'license_software_download_request'

               },

               {

                   label: 'Configuration Assistance',

                   value: 'configuration_assistance'

               }

           ],
           inquiry: [

               {

                   label: 'Inquiry',

                   value: 'inquiry'

               },

               {

                   label: 'No MA Coverage',

                   value: 'no_ma_coverage'

               }

           ],



           adhoc_request: [

               {

                   label: 'Hardware Replacement',

                   value: 'hardware_replacement'

               },

               {

                   label: 'Software Fault Remediation',

                   value: 'software_fault_remediation'

               },

               {

                   label: 'Non Hardware Fault - Troubleshooting',

                   value: 'troubleshooting'

               },

               {

                   label: 'Call Management - Hardware Replacement',

                   value: 'call_management_hardware_replacement'

               },

               {

                   label: 'Call Management - Software Remediation',

                   value: 'call_management_software_remediation'
               },

               {

                   label: 'Call Management - UPS Hardware Replacement',
                   value: 'call_management_ups_hardware_replacement'

               }

           ]

       };

       return JSON.stringify(categoryMapping[opsCategory] || []);

   },

   type: 'NCS_FaultCategoryAjax'

});

Client Script (onChange on (u_operations_category)

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || oldValue == newValue)
       return;
   var field = 'u_fault_category';
   // Reset field
   g_form.setValue(field, '');
   g_form.clearOptions(field);
   g_form.addOption(field, '', '-- None --', 0);
   if (!newValue)
       return;
   var selectedCategory = newValue;
   var ga = new GlideAjax('NCS_FaultCategoryAjax');
   ga.addParam('sysparm_name', 'getFaultCategoriesByOpsCategory');
   ga.addParam('sysparm_ops_category', selectedCategory);
   ga.getXMLAnswer(function(answer) {
       // Ignore old AJAX responses
       if (g_form.getValue('u_operations_category') != selectedCategory)
           return;
       if (!answer)
           return;
       var choices = JSON.parse(answer);
       g_form.clearOptions(field);
       g_form.addOption(field, '', '-- None --', 0);
       for (var i = 0; i < choices.length; i++) {
           g_form.addOption(
               field,
               choices[i].value,
               choices[i].label
           );
       }
       g_form.setValue(field, '');
   });
}


*********************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb



View solution in original post

Abhit
Tera Guru

@harshal001 

Script Include:

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

    getFaultCategoriesByOpsCategory: function() {
        var opsCategory = this.getParameter('sysparm_ops_category');

        var categoryMapping = {
            'fault': [{
                    label: 'Hardware Replacement',
                    value: 'hardware_replacement'
                },
                {
                    label: 'Software Fault Remediation',
                    value: 'Software Fault Remediation'
                },
                {
                    label: 'Non Hardware Fault - Troubleshooting',
                    value: 'troubleshooting'
                }
            ],
            'service_request': [{
                    label: 'Call Management - Hardware Replacement',
                    value: 'call_management_Hardware_Replacement'
                },
                {
                    label: 'Call Management - Software Remediation',
                    value: 'call_management_Others'
                },
                {
                    label: 'Call Management - UPS Hardware Replacement',
                    value: 'call_management_UPS_Hardware_Replacement'
                },
                {
                    label: 'Call Management - Fans & Cable Replacement',
                    value: 'call_management_Fans_Cable_Replacement'
                },
                {
                    label: 'Call Management - UPS',
                    value: 'call_management_ups'
                },
                {
                    label: 'Call Management - Service Request',
                    value: 'call_management_Service_Request'
                }
            ],
            'standby_request': [{
                    label: 'License or Software Download Request',
                    value: 'license_or_Software_Download_Request'
                },
                {
                    label: 'Upgrade/Patch Services',
                    value: 'upgrade_patch_Services'
                },
                {
                    label: 'Configuration Assistance',
                    value: 'configuration_assistance'
                },
                {
                    label: 'Preventive Maintenance Service',
                    value: 'preventive_maintenance_service'
                }
            ],
            'inquiry': [{
                    label: 'Inquiry',
                    value: 'Inquiry'
                },
                {
                    label: 'No MA Coverage',
                    value: 'no_ma_coverage'
                },
                {
                    label: 'Vulnerability Check Only',
                    value: 'vulnerability_check_only'
                }
            ],
            'sadhoc_request': [{
                    label: 'Hardware Replacement',
                    value: 'hardware_replacement'
                },
                {
                    label: 'Software Fault Remediation',
                    value: 'Software Fault Remediation'
                },
                {
                    label: 'Non Hardware Fault - Troubleshooting',
                    value: 'troubleshooting'
                },
                {
                    label: 'Call Management - Hardware Replacement',
                    value: 'call_management_Hardware_Replacement'
                },
                {
                    label: 'Call Management - Software Remediation',
                    value: 'call_management_Others'
                },
                {
                    label: 'Call Management - UPS Hardware Replacement',
                    value: 'call_management_UPS_Hardware_Replacement'
                },
                {
                    label: 'Call Management - Fans & Cable Replacement',
                    value: 'call_management_Fans_Cable_Replacement'
                },
                {
                    label: 'Call Management - UPS',
                    value: 'call_management_ups'
                },
                {
                    label: 'Call Management - Service Request',
                    value: 'call_management_Service_Request'
                },
                {
                    label: 'License or Software Download Request',
                    value: 'license_or_Software_Download_Request'
                },
                {
                    label: 'Upgrade/Patch Services',
                    value: 'upgrade_patch_Services'
                },
                {
                    label: 'Configuration Assistance',
                    value: 'configuration_assistance'
                },
                {
                    label: 'Preventive Maintenance Service',
                    value: 'preventive_maintenance_service'
                },
                {
                    label: 'Inquiry',
                    value: 'Inquiry'
                },
                {
                    label: 'No MA Coverage',
                    value: 'no_ma_coverage'
                },
                {
                    label: 'Vulnerability Check Only',
                    value: 'vulnerability_check_only'
                },
                {
                    label: 'Standby Request',
                    value: 'standby_request'
                }
            ]
        };
        var result = categoryMapping[opsCategory] || [];
        return JSON.stringify(result);
    },
    type: 'NCS_FaultCategoryAjax'
});

 

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || oldValue === newValue)
        return;
    var field = 'u_fault_category';
    // Reset field
    g_form.setValue(field, '');
    g_form.clearOptions(field);
    g_form.addOption(field, '', '-- None --', 0);
    if (!newValue)
        return;
    var selectedCategory = newValue;
    var ga = new GlideAjax('NCS_FaultCategoryAjax');
    ga.addParam('sysparm_name', 'getFaultCategoriesByOpsCategory');
    ga.addParam('sysparm_ops_category', selectedCategory);
    ga.getXMLAnswer(function(answer) {
        // Ignore stale Ajax responses
        if (g_form.getValue('u_operations_category') != selectedCategory)
            return;
        if (!answer)
            return;
        var choices = JSON.parse(answer);
        g_form.clearOptions(field);
        g_form.addOption(field, '', '-- None --', 0);
        choices.forEach(function(choice) {
            g_form.addOption(field, choice.value, choice.label);
        });
    });
}

 

Make sure there is no space in the choice values.

View solution in original post

17 REPLIES 17

Hey @harshal001 

Script Include (Client Callable)

var NCS_FaultCategoryAjax = Class.create();
NCS_FaultCategoryAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   getFaultCategoriesByOpsCategory: function() {
       var opsCategory = this.getParameter('sysparm_ops_category');
       var categoryMapping = {
           fault: [
               {
                   label: 'Hardware Replacement',
                   value: 'hardware_replacement'
               },

               {

                   label: 'Software Fault Remediation',
                   value: 'software_fault_remediation'

               },

               {

                   label: 'Non Hardware Fault - Troubleshooting',

                   value: 'troubleshooting'

               },

               {

                   label: 'Call Management - Hardware Replacement',

                   value: 'call_management_hardware_replacement'

               },

               {

                   label: 'Call Management - Software Remediation',

                   value: 'call_management_software_remediation'

               },

               {

                   label: 'Call Management - UPS Hardware Replacement',

                   value: 'call_management_ups_hardware_replacement'

               }

           ],



           service_request: [

               {

                   label: 'Call Management - UPS',

                   value: 'call_management_ups'

               },

               {

                   label: 'Call Management - Service Request',

                   value: 'call_management_service_request'

               },

               {

                   label: 'Standby Request',

                   value: 'standby_request'

               },

               {

                   label: 'Vulnerability Check Only',

                   value: 'vulnerability_check_only'

               },

               {

                   label: 'Preventive Maintenance Service',

                   value: 'preventive_maintenance_service'

               },

               {

                   label: 'Upgrade/Patch Services',

                   value: 'upgrade_patch_services'

               },

               {

                   label: 'License or Software Download Request',

                   value: 'license_software_download_request'

               },

               {

                   label: 'Configuration Assistance',

                   value: 'configuration_assistance'

               }

           ],
           inquiry: [

               {

                   label: 'Inquiry',

                   value: 'inquiry'

               },

               {

                   label: 'No MA Coverage',

                   value: 'no_ma_coverage'

               }

           ],



           adhoc_request: [

               {

                   label: 'Hardware Replacement',

                   value: 'hardware_replacement'

               },

               {

                   label: 'Software Fault Remediation',

                   value: 'software_fault_remediation'

               },

               {

                   label: 'Non Hardware Fault - Troubleshooting',

                   value: 'troubleshooting'

               },

               {

                   label: 'Call Management - Hardware Replacement',

                   value: 'call_management_hardware_replacement'

               },

               {

                   label: 'Call Management - Software Remediation',

                   value: 'call_management_software_remediation'
               },

               {

                   label: 'Call Management - UPS Hardware Replacement',
                   value: 'call_management_ups_hardware_replacement'

               }

           ]

       };

       return JSON.stringify(categoryMapping[opsCategory] || []);

   },

   type: 'NCS_FaultCategoryAjax'

});

Client Script (onChange on (u_operations_category)

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || oldValue == newValue)
       return;
   var field = 'u_fault_category';
   // Reset field
   g_form.setValue(field, '');
   g_form.clearOptions(field);
   g_form.addOption(field, '', '-- None --', 0);
   if (!newValue)
       return;
   var selectedCategory = newValue;
   var ga = new GlideAjax('NCS_FaultCategoryAjax');
   ga.addParam('sysparm_name', 'getFaultCategoriesByOpsCategory');
   ga.addParam('sysparm_ops_category', selectedCategory);
   ga.getXMLAnswer(function(answer) {
       // Ignore old AJAX responses
       if (g_form.getValue('u_operations_category') != selectedCategory)
           return;
       if (!answer)
           return;
       var choices = JSON.parse(answer);
       g_form.clearOptions(field);
       g_form.addOption(field, '', '-- None --', 0);
       for (var i = 0; i < choices.length; i++) {
           g_form.addOption(
               field,
               choices[i].value,
               choices[i].label
           );
       }
       g_form.setValue(field, '');
   });
}


*********************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb



what is happening here when i select any First choice in Operations category then fault choice displayed as mapping done, but when switch the choice for operation category then in fault category choices are not displayed without reloading the page.... This issue I am getting now.

Even I have try with dictionary table configuration Depend on field Checkbox checked 

This way Fault, Service Request, Inquiry is mapping directly using dictionary choice dependent field. but for adhoc only i am getting this issue

Hey @harshal001 

From your description, it looks like the Script Include is working correctly because the Fault Category choices are populated the first time you select an Operations Category. The problem occurs only when switching to another Operations Category without reloading the form.

A few questions and observations:

  1. Is u_fault_category a Choice field or a Reference field?
  2. Are you testing this in Classic UI, Service Portal, or Workspace?

You mentioned that enabling the Dependent field in the Dictionary works correctly for Fault, Service Request, and Inquiry, but not for Adhoc. That suggests the issue is likely related to the field configuration or choice records rather than the GlideAjax call itself.

Please verify the following:

  1. Dependent Choice Configuration

  Check whether the Adhoc choices exist in the sys_choice table with the correct    Dependent Value.

  If Adhoc does not have matching dependent choice records, the field will appear blank after the parent value changes.

2. Client Script

Before repopulating the choices, reset the field value and clear the existing options:

g_form.setValue('u_fault_category', '');

g_form.clearOptions('u_fault_category');

g_form.addOption('u_fault_category', '', '-- None --', 0);

3. GlideAjax Response

Add a console.log(response); inside the callback to confirm that the Script Include is returning the expected JSON when Adhoc is selected.

4. Conflicting Logic

Check for any other Client Scripts, UI Policies, or Dictionary dependencies that may be clearing or filtering the choices after your callback executes.

Since the issue occurs only for Adhoc, my first suspicion would be that the dependent choice configuration or the mapping for Adhoc is incomplete or conflicting with the dynamically added options.

Could you also share:

  1. The choice records for Operations Category = Adhoc.
  2. Whether u_fault_category is using both Dictionary dependent choices and GlideAjax at the same time?

Using both approaches together can sometimes result in the dynamically added options being removed by the platform's built-in dependent choice filtering.

*********************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb





@vaishali231 
u_operations_category and U_fault_category is String type Fields.

I am testing it on Classic UI(Native Ui) in incident.do form

  1. The choice records for Operations Category = Adhoc.
    -- For Adhoc = all choices should be Display in Fault category in problem statement i have attached Screen shot.
  2. Whether u_fault_category is using both Dictionary dependent choices and GlideAjax at the same time? 
    -- previous i have done that but later i remove dependent choice and completely doing on script only.