Record producer category and subcategory Dependency by using onchnage client script

Yougander patel
Tera Contributor

 

i have a variable called category with choices software ,hardware and Security and i have another variable called subcategory with choices email, phone, iPad, keyboard, monitor, Spam so here when i select category = software then in subcategory choices should visible only email and phone ,when i select category= hardware then subcategory choices should only visible iPad, keyboard, monitor and when i select category =security then i should select only spam this can be done by using Script Include and On change client script in ServiceNow get me full code for script include and onchange client script code 

 

Below code is exact backend values of choices and catagory and subcatgory  the below code is not working  please on change client script give me another way of approch via script inclue and Glideajax code to full the task

 

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

var category = g_form.getValue('what_are_you_reporting_the_issue_with');
var subcategory = g_form.getValue('sub_catagory');

var subcategories = [];

switch (category) {
case 'software':
subcategories = ['email', 'eway', 'password reset', 'proclaim', 'visual files'];
break;
case 'hardware':
subcategories = ['iPad', 'keyboard', 'monitor', 'headset', 'laptop', 'mobile phone', 'mouse', 'monitor', 'pc', 'printer', 'phone', 'scanner', 'thin client', 'WiFi'];
break;
case 'security':
subcategories = ['spam', 'phishing', 'emailquarantinerelease', 'other'];
break;
default:
subcategories = [];
}

// Clear the existing subcategory choices
g_form.clearOptions('sub_category');

// Add the new subcategory choices
for (var i = 0; i < subcategories.length; i++) {
var value = subcategories[i];
var label = subcategories[i];

g_form.addOption('sub_category', value, label);
}

// Set the subcategory to the first option if it's no longer valid
if (subcategories.indexOf(subcategory) === -1) {
g_form.setValue('sub_category', subcategories[0]);
}
}

7 REPLIES 7

AnveshKumar M
Tera Sage
Tera Sage

Hi @Yougander patel ,

 

1. Add a None option to the Category variable like below.

AnveshKumarM_0-1681373180109.png

 

2. Try this onChange client script,

 

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

    var subcategory = g_form.getValue('sub_catagory');

    var subcategories;

    switch (newValue) {
        case 'software':
            subcategories = ['email', 'eway', 'password reset', 'proclaim', 'visual files'];
            break;
        case 'hardware':
            subcategories = ['iPad', 'keyboard', 'monitor', 'headset', 'laptop', 'mobile phone', 'mouse', 'monitor', 'pc', 'printer', 'phone', 'scanner', 'thin client', 'WiFi'];
            break;
        case 'security':
            subcategories = ['spam', 'phishing', 'emailquarantinerelease', 'other'];
            break;
        default:
            subcategories = [];
    }

    // Clear the existing subcategory choices
    g_form.clearOptions('sub_category');

    // Add the new subcategory choices
    for (var i = 0; i < subcategories.length; i++) {
        g_form.addOption('sub_category', subcategories[i], subcategories[i]);
    }

    // Set the subcategory to the first option if it's no longer valid
    if (subcategories.indexOf(subcategory) === -1) {
        g_form.setValue('sub_category', subcategories[0]);
    }
}

 

AnveshKumarM_1-1681373271586.png

 

 

 

 

Thanks,
Anvesh

Sorry Anvesh the client script is not working !,i select selected ui type= all,inclide none and same backend values are same but still not working

 @Yougander patel ,

Let's debug this, paste the below script in your onChange client script and check whether the alert messages are coming in when you change the category values, and let me know, if the alert message is displayed which value is displayed.

 

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

    var subcategory = g_form.getValue('sub_catagory');
    alert(6);
    var subcategories;

    switch (newValue) {
        case 'software':
            alert(1);
            subcategories = ['email', 'eway', 'password reset', 'proclaim', 'visual files'];
            break;
        case 'hardware':
            alert(2);
            subcategories = ['iPad', 'keyboard', 'monitor', 'headset', 'laptop', 'mobile phone', 'mouse', 'monitor', 'pc', 'printer', 'phone', 'scanner', 'thin client', 'WiFi'];
            break;
        case 'security':
            alert(3);
            subcategories = ['spam', 'phishing', 'emailquarantinerelease', 'other'];
            break;
        default:
            alert('Default');
            subcategories = [];
    }

    // Clear the existing subcategory choices
    g_form.clearOptions('sub_category');

    // Add the new subcategory choices
    for (var i = 0; i < subcategories.length; i++) {
        g_form.addOption('sub_category', subcategories[i], subcategories[i]);
    }

    // Set the subcategory to the first option if it's no longer valid
    if (subcategories.indexOf(subcategory) === -1) {
        g_form.setValue('sub_category', subcategories[0]);
    }
}

 

 

Thanks,
Anvesh

first alert =5 ; second alert =6 ;third alert =1 ;still i can  an see all the choices in the subcategory

 

for hardware;6,2

for security:6,3,