Dependent values getting erased if we are using g_form.removeOption for onchange client script

SamiranD
Tera Contributor

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

 

13 REPLIES 13

SamiranD
Tera Contributor

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

    if (isLoading || newValue == '') {

        //g_form.clearOptions('request_sub_type');

        // g_form.addOption('request_sub_type', '', '-- None --');

        return;

    }

 

    if (g_form.getValue('request_type') == 'service_request') {

        g_form.

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'performance_monitoring');

        g_form.removeOption('request_sub_type', 'capacity_planning');

        g_form.removeOption('request_sub_type', 'scalability_requests');

        g_form.removeOption('request_sub_type', 'vulnerability_management');

        g_form.removeOption('request_sub_type', 'compliance_request');

        g_form.removeOption('request_sub_type', 'configuration_changes');

        g_form.removeOption('request_sub_type', 'deployments');

        g_form.removeOption('request_sub_type', 'schedule_maintenance');

        g_form.removeOption('request_sub_type', 'system_outage');

        g_form.removeOption('request_sub_type', 'network_issues');

        g_form.removeOption('request_sub_type', 'hardware_failures');

        g_form.removeOption('request_sub_type', 'application_errors');

 

        g_form.addOption('request_sub_type', 'hardware_provisioning');

        g_form.addOption('request_sub_type', 'access_management');

        g_form.addOption('request_sub_type', 'software_installation');

        g_form.addOption('request_sub_type', 'network_requests');

        g_form.addOption('request_sub_type', 'information_request');

        g_form.addOption('request_sub_type', 'onboarding');

 

 

    } else if (g_form.getValue('request_type') == 'capacity_management') {

 

        g_form.addOption('request_sub_type', 'resource_allocation');

        g_form.addOption('request_sub_type', 'performance_monitoring');

        g_form.addOption('request_sub_type', 'capacity_planning');

        g_form.addOption('request_sub_type', 'scalability_requests');

        g_form.removeOption('request_sub_type', 'vulnerability_management');

        g_form.removeOption('request_sub_type', 'compliance_request');

        g_form.removeOption('request_sub_type', 'configuration_changes');

        g_form.removeOption('request_sub_type', 'deployments');

        g_form.removeOption('request_sub_type', 'schedule_maintenance');

        g_form.removeOption('request_sub_type', 'system_outage');

        g_form.removeOption('request_sub_type', 'network_issues');

        g_form.removeOption('request_sub_type', 'hardware_failures');

        g_form.removeOption('request_sub_type', 'application_errors');

         g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'access_management');

        g_form.removeOption('request_sub_type', 'software_installation');

        g_form.removeOption('request_sub_type', 'network_requests');

        g_form.removeOption('request_sub_type', 'information_request');

        g_form.removeOption('request_sub_type', 'onboarding');

 

 

 

       

 

    } else if (g_form.getValue('request_type') == 'security_management') {

 

        g_form.addOption('request_sub_type', 'vulnerability_management');

        g_form.addOption('request_sub_type', 'compliance_request');

 

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'performance_monitoring');

        g_form.removeOption('request_sub_type', 'capacity_planning');

        g_form.removeOption('request_sub_type', 'scalability_requests');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'configuration_changes');

        g_form.removeOption('request_sub_type', 'deployments');

        g_form.removeOption('request_sub_type', 'schedule_maintenance');

        g_form.removeOption('request_sub_type', 'system_outage');

        g_form.removeOption('request_sub_type', 'network_issues');

        g_form.removeOption('request_sub_type', 'hardware_failures');

        g_form.removeOption('request_sub_type', 'application_errors');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'access_management');

        g_form.removeOption('request_sub_type', 'software_installation');

        g_form.removeOption('request_sub_type', 'network_requests');

        g_form.removeOption('request_sub_type', 'information_request');

        g_form.removeOption('request_sub_type', 'onboarding');

 

    } else if (g_form.getValue('request_type') == 'change_management') {

        g_form.addOption('request_sub_type', 'configuration_changes');

        g_form.addOption('request_sub_type', 'deployments');

        g_form.addOption('request_sub_type', 'schedule_maintenance');

 

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'performance_monitoring');

        g_form.removeOption('request_sub_type', 'capacity_planning');

        g_form.removeOption('request_sub_type', 'scalability_requests');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'system_outage');

        g_form.removeOption('request_sub_type', 'network_issues');

        g_form.removeOption('request_sub_type', 'hardware_failures');

        g_form.removeOption('request_sub_type', 'application_errors');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', '   access_management');

        g_form.removeOption('request_sub_type', 'software_installation');

        g_form.removeOption('request_sub_type', 'network_requests');

        g_form.removeOption('request_sub_type', 'information_request');

        g_form.removeOption('request_sub_type', 'onboarding');

        g_form.removeOption('request_sub_type', 'vulnerability_management');

        g_form.removeOption('request_sub_type', 'compliance_request');

 

    } else if (g_form.getValue('request_type') == 'lower_env_sprt') {

        g_form.addOption('request_sub_type', 'system_outage');

        g_form.addOption('request_sub_type', 'network_issues');

        g_form.addOption('request_sub_type', 'hardware_failures');

        g_form.addOption('request_sub_type', 'application_errors');

 

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'resource_allocation');

        g_form.removeOption('request_sub_type', 'performance_monitoring');

        g_form.removeOption('request_sub_type', 'capacity_planning');

        g_form.removeOption('request_sub_type', 'scalability_requests');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'hardware_provisioning');

        g_form.removeOption('request_sub_type', 'access_management');

        g_form.removeOption('request_sub_type', 'software_installation');

        g_form.removeOption('request_sub_type', 'network_requests');

        g_form.removeOption('request_sub_type', 'information_request');

        g_form.removeOption('request_sub_type', 'onboarding');

        g_form.removeOption('request_sub_type', 'vulnerability_management');

        g_form.removeOption('request_sub_type', 'compliance_request');

        g_form.removeOption('request_sub_type', 'configuration_changes');

        g_form.removeOption('request_sub_type', 'deployments');

        g_form.removeOption('request_sub_type', 'schedule_maintenance');

    }

    //Type appropriate comment here, and begin script below

 

}

Oh dear! Right off the bat, there's a "g_form." line that is not complete after your first if statement. That needs to be removed. 

 

I don't know how many items are in this list, but a cleaner approach would be to wipe out all selections (like it looks like you started to do) and then just add the ones that are needed for each condition. You don't know if a person is switching from one request type to another, and some of those remove statements will end up erroring out if they don't exist.

Ankur Bawiskar
Tera Patron
Tera Patron

@SamiranD 

Similar to incident category and subcategory you can have variables with dependency in catalog

this link has solution

ServiceNow – Dependent Variables on Record Producer in Service Catalog 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I believe that solution will only work if these are values that are pulled from a table. This appears to not be the case here.

I don't have the options in a table.

also,

whenever i am changing the request type the sub request type values are getting cleared from the second time onwards. any idea how can i fix this?