- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 07:01 AM
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:
Regards,
Lucky
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 07:34 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 07:07 AM - edited 04-01-2025 07:12 AM
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');
}
}
Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 07:32 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 07:34 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader