Conditional Display of Choices in ServiceNow Catalog Item Variables

Nikhitha Mohan
Tera Contributor

Hi,

I have created two multiple-choice variables in a catalog item:

  1. Laptop/Desktop (laptop/desktop) with two choices: Laptop and Desktop.
  2. 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?

NikhithaMohan_0-1733383722633.png

 

below the variable added,
 

NikhithaMohan_0-1733383531691.png

 

 

6 REPLIES 6

Shruti D
Tera Guru

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




Hi @Shruti D , tried this script but it is not working

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!

Community Alums
Not applicable

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