Need help in on Change CS

Lucky1
Tera Guru

Hello all,

 

I have created two variables Block and Department and they have their own choices.

Based on Block selected, the department variables values has to be filtered.

I have used on Change CS on Block variable and achieved this. But is there any other way we can do it simple?

 

Code I have used is below:

 

Lucky1_0-1743515990952.png

 

 

Regards,

Lucky

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

it could be simplified like this

Going forward if you want to add/remove the choices then simply update the departmentOptions JSON and no script change required.

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

    // Define department options based on the selected Block
    var departmentOptions = {
        'cafeteria': [
            { value: '', label: '-- None --' },
            { value: 'project_lead', label: 'Project Lead' },
            { value: 'team_lead', label: 'Team Lead' },
            { value: 'team_member', label: 'Team Member' },
            { value: 'admin', label: 'Admin' }
        ],
        'main_campus': [
            { value: 'project_lead', label: 'Project Lead' },
            { value: 'team_lead', label: 'Team Lead' },
            { value: 'team_member', label: 'Team Member' },
            { value: 'admin', label: 'Admin' }
        ]
        // Add more blocks and their corresponding departments as needed
    };

    // Clear existing options and add new options
    updateOptions('u_department', departmentOptions[newValue] || []);
}

function updateOptions(variableName, options) {
    g_form.clearOptions(variableName);
    options.forEach(function(option) {
        g_form.addOption(variableName, option.value, option.label);
    });
}

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

View solution in original post

3 REPLIES 3

Rushi Savarkar
Kilo Sage

Hello @Lucky1 

Please give the detailed requirement as if

Block = SEZ , Which values should be populated

Block = Cafeteria, Which values should be populated

Block = Main Campus, Which values should be populated

Till then you can try with below logic

The syntax of addOption and removeOption method is:

g_form.addOption("variable_name","choice_value","choice_label");

g_form.removeOption("variable_name","choice_value","choice_label");

Below is the sample script that I used for one of my project

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
		g_form.removeOption('role_name1', 'NGK_RJ', 'NGK_RJ');
        g_form.removeOption('role_name1', 'RGK_LK', 'RGK_LK');
		g_form.removeOption('role_name1', 'consumer_reporting', 'consumer_reporting');
        g_form.removeOption('role_name1', 'customer_reporting', 'customer_reporting');
        return;
    }
	alert(newValue);
    if (newValue == 'NAWM') {

        g_form.addOption('role_name1', 'NGK_RJ', 'NGK_RJ');
        g_form.addOption('role_name1', 'RGK_LK', 'RGK_LK');

        g_form.removeOption('role_name1', 'consumer_reporting', 'consumer_reporting');
        g_form.removeOption('role_name1', 'customer_reporting', 'customer_reporting');

    } else if (newValue == 'SCReport') {
        g_form.addOption('role_name1', 'consumer_reporting', 'consumer_reporting');
        g_form.addOption('role_name1', 'customer_reporting', 'customer_reporting');

        g_form.removeOption('role_name1', 'NGK_RJ', 'NGK_RJ');
        g_form.removeOption('role_name1', 'RGK_LK', 'RGK_LK');

    }

}

 

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

please share your script here and not image

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

it could be simplified like this

Going forward if you want to add/remove the choices then simply update the departmentOptions JSON and no script change required.

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

    // Define department options based on the selected Block
    var departmentOptions = {
        'cafeteria': [
            { value: '', label: '-- None --' },
            { value: 'project_lead', label: 'Project Lead' },
            { value: 'team_lead', label: 'Team Lead' },
            { value: 'team_member', label: 'Team Member' },
            { value: 'admin', label: 'Admin' }
        ],
        'main_campus': [
            { value: 'project_lead', label: 'Project Lead' },
            { value: 'team_lead', label: 'Team Lead' },
            { value: 'team_member', label: 'Team Member' },
            { value: 'admin', label: 'Admin' }
        ]
        // Add more blocks and their corresponding departments as needed
    };

    // Clear existing options and add new options
    updateOptions('u_department', departmentOptions[newValue] || []);
}

function updateOptions(variableName, options) {
    g_form.clearOptions(variableName);
    options.forEach(function(option) {
        g_form.addOption(variableName, option.value, option.label);
    });
}

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