Hide options from Select box variable depending on the option selected from previous select box

Krishna Priya
Tera Contributor
Hide options from Select box variable depending on the option selected from previous select box
 

My requirement is that i have two select box variables Platform name and work request depending on the selection in variable Platform name, the options in work request should be shown or hidden.

 

Please advise how to achieve this via client script

 

Below script implemented but if i change selection multiple times its not working. Please advice where the issue.

 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var platform = g_form.getValue('Platform_name');
    var work_request = g_form.setValue('work_request');

    if (platform == "Email") {
        g_form.removeOption('work_request', "domain");
        g_form.addOption('work_request', "Data ");
        g_form.addOption('work_request', "Email");

    } else if (platform == "Endpoint") {
        g_form.removeOption('work_request', "Email");
        g_form.addOption('work_request', "Data");
        g_form.addOption('work_request', "domain");
    }
}
9 REPLIES 9

Sagar Pagar
Tera Patron

Hi @Krishna Priya,

 

I would suggests to use advanced reference qualifier to control visibility of options in select box variable instead of Catalog client scripts.

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Sandeep Rajput
Tera Patron
Tera Patron

@Krishna Priya I recommend you using UI Policy with script field checked to achieve this functionality. Let me know if you need any help in creating the UI Policy for this purpose.

yes please help on this.

Aniket Chavan
Tera Sage
Tera Sage

Hello @Krishna Priya ,

Please give a try to the script below and see how it works for you.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var platform = g_form.getValue('Platform_name');

    // Clear existing query on 'work_request' variable
    g_form.getReference('work_request').clearOptions();

    // Set a new query based on the selected 'Platform_name'
    if (platform === 'Email') {
        g_form.getReference('work_request').addQuery('category', '=', 'Email');
    } else if (platform === 'Endpoint') {
        g_form.getReference('work_request').addQuery('category', '=', 'Endpoint');
    }
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket