- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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.
please provide you guidance. Thank you
#client-script # Servicenow #glideajax
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
@Abhit Are you able to see fault choice while keep changing operations category ?
Fault category is String type Field
Operations category is String type field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Yes, it does.
The Fault Choice dynamically changes based on the selected Operations Category without requiring the form to be saved.
However, once the form is saved and reloaded, the Fault Choice retains the previously selected value and does not display the updated list according to the current Operations Category.
To ensure all relevant choices are displayed based on the selected Operations Category, you need to modify the isLoading condition in the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Abhit Since our Fault category is Sting type field. So, I have made all choice under that Fault category from dictionary table made inactive and From the Script include passing all choice as array with their respective mapping. and using on change client script making call, Now it is working as expected.
So Is it is correct approach for implementing this scenario??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
As your requirement is a straightforward dependency (similar to Incident Category/Subcategory), you could leverage the OOTB dependent choice functionality instead of implementing Script Includes and client scripts. This would simplify the solution and avoid unnecessary scripting.
Note: In this approach, choices automatically filter based on the selected Operations Category.
The scripting approach is more suitable when the dependency logic is complex or dynamic. In such cases, ensure the logic runs not only on onChange but also on onLoad to maintain a consistent end-user experience.
Also, instead of inactivating all choices, it is generally recommended to keep choices active and control their visibility dynamically through the script.
Note: If the script does not execute on onLoad, all active choices will be visible by default.
~Ak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Actually, I have used this method before. However, if you look at my requirement, when the Operations Category is Adhoc Request, all Fault Category choices should be displayed.
I have already used the dependent choice functionality for Fault, Service Request, and Inquiry, and it works correctly for those categories. The challenge is with Adhoc Request, where all Fault Category choices need to be available.
Currently, when I select any other Operations Category, the corresponding Fault Category choices are displayed correctly. However, when I select Adhoc Request, no Fault Category choices are displayed. This is the issue I am facing.