Conditional Display of Choices in ServiceNow Catalog Item Variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 11:29 PM
Hi,
I have created two multiple-choice variables in a catalog item:
- Laptop/Desktop (laptop/desktop) with two choices: Laptop and Desktop.
- Mode of Laptop Delivery (mode_of_laptop_delivery) with two choices: Shipping the Laptop and Picking it up from the Office Location.
The requirement is as follows:
- If the user selects Laptop in the laptop/desktop field, both options in mode_of_laptop_delivery should be displayed.
- If the user selects Desktop, only Picking it up from the Office Location should be displayed, and the other option should be hidden.
I tried implementing this with the following script, but it’s not working. Could you please assist me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 12:26 AM - edited 12-05-2024 01:33 AM
Hello @Nikhitha Mohan ,
Please try the below onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var LaptopDesktop = g_form.getValue('laptop_desktop');
var Mode = g_form.getValue('mode_of_laptop_delivery');
if (LaptopDesktop == 'desktop') {
// If 'Desktop' is selected, only add the 'Picking it up from the Office Location' option
g_form.removeOption('mode_of_laptop_delivery', 'shipping_the_laptop');
g_form.addOption('mode_of_laptop_delivery', 'picking_it_up_from_the_Office _location', 'Picking it up from the Office Location');
}
else if (LaptopDesktop == 'laptop') {
// If 'Laptop' is selected, add both options back
g_form.addOption('mode_of_laptop_delivery', 'shipping_the_laptop', 'Shipping the Laptop');
g_form.addOption('mode_of_laptop_delivery', 'picking_it_up_from_the_office _location', 'Picking it up from the Office Location');
}
}
Please Mark Correct ✔️if this solves your query and also mark Helpful 👍 if you find my response worthy based on the impact.
Regards,
Shruti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 12:50 AM
Hi @Shruti D , tried this script but it is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 01:35 AM
Hello @Nikhitha Mohan
Please ensure if you added the correct labels and backend values in the Script.
As I've tried the same and its working at my end.
Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 01:05 AM
Hi @Nikhitha Mohan ,
1)please check front end and back end value of choices.
try with this code
function onChange(control, oldValue, newValue, isLoading) {
// Ensure the form is not in loading state
if (isLoading) return;
// Get the mode_of_laptop_delivery field
var modeOfDelivery = g_form.getControl('mode_of_laptop_delivery');
// Check the selected value for the Laptop/Desktop field
if (newValue == 'Laptop') {
// If Laptop is selected, show both options for delivery
g_form.setVisible('mode_of_laptop_delivery', true); // Show the field
g_form.setChoiceValues('mode_of_laptop_delivery', ['Shipping the Laptop', 'Picking it up from the Office Location']);
} else if (newValue == 'Desktop') {
// If Desktop is selected, only show the "Picking it up" option
g_form.setVisible('mode_of_laptop_delivery', true); // Show the field
g_form.setChoiceValues('mode_of_laptop_delivery', ['Picking it up from the Office Location']);
}
}
try with this code . i have tried in PDI its working .THANK YOU @Nikhitha Mohan