vaishali231
Kilo Sage

Hey @harshal001 
try this :

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);
        });

    });
}

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

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  I try this way as well but did display choice in fault cate.
If possible, could please share script include and client script both

 

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

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