Dependent values getting erased if we are using g_form.removeOption for onchange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 06:26 AM
I have two variables, request type and subrequest type. values of sub request type are dependent on request type. Now I have added all the question choices for sub request type and using g_form.removeOption to hide the values based on selected requested type value. But for the first selection of request type i am getting the desired sub request type values, but for subsequent slections i am not able to see any values in sub request type.
Any one can help please
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 09:45 PM
do this
1) when onChange runs clear the options
2) then based on value add the options
try this -> give correct variable names, correct choice value and choice labels while adding
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Clear all options for sub_request_type
g_form.clearOptions('request_sub_type');
g_form.addOption('request_sub_type', '', '-- None --');
if (g_form.getValue('request_type') == 'service_request') {
g_form.addOption('request_sub_type', 'hardware_provisioning', 'Hardware Provisioning');
g_form.addOption('request_sub_type', 'access_management', 'Access Management');
g_form.addOption('request_sub_type', 'software_installation', 'Software Installation');
g_form.addOption('request_sub_type', 'network_requests', 'Network Requests');
g_form.addOption('request_sub_type', 'information_request', 'Information Request');
g_form.addOption('request_sub_type', 'onboarding', 'Onboarding');
} else if (g_form.getValue('request_type') == 'capacity_management') {
g_form.addOption('request_sub_type', 'resource_allocation', 'Resource Allocation');
g_form.addOption('request_sub_type', 'performance_monitoring', 'Performance Monitoring');
g_form.addOption('request_sub_type', 'capacity_planning', 'Capacity Planning');
g_form.addOption('request_sub_type', 'scalability_requests', 'Scalability Requests');
} else if (g_form.getValue('request_type') == 'security_management') {
g_form.addOption('request_sub_type', 'vulnerability_management', 'Vulnerability Management');
g_form.addOption('request_sub_type', 'compliance_request', 'Compliance Request');
} else if (g_form.getValue('request_type') == 'change_management') {
g_form.addOption('request_sub_type', 'configuration_changes', 'Configuration Changes');
g_form.addOption('request_sub_type', 'deployments', 'Deployments');
g_form.addOption('request_sub_type', 'schedule_maintenance', 'Schedule Maintenance');
} else if (g_form.getValue('request_type') == 'lower_env_sprt') {
g_form.addOption('request_sub_type', 'system_outage', 'System Outage');
g_form.addOption('request_sub_type', 'network_issues', 'Network Issues');
g_form.addOption('request_sub_type', 'hardware_failures', 'Hardware Failures');
g_form.addOption('request_sub_type', 'application_errors', 'Application Errors');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 09:58 PM
I have added the choices in the question choices under the related list of variable request_sub_type. Do i also need to write the variable label in the code? i.e-
g_form.addOption('request_sub_type', 'application_errors', 'Application Errors');
the 3rd parameter also i have to include in my code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 10:03 PM
yes 2nd parameter is choice value and 3rd is choice label
So ensure the choice labels are correct and then it will work
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 12:38 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader