Need help in script debugging for hide and show checkbox variable

MayurChaudhari
Tera Contributor

In servicenow for catalog item i have created a 2 select box varibale like 1st one is Location and its choices are "Richmond", "Tampa", "Texas", "Nashville". 2nd one select box varibale is What computer Type? and its choices are "Laptop", "Desktop", "Docking Station". And my requirement is like if I select "Richmond" in Location field the in field What computer Type i have to show "Laptop", "Desktop" options only and if selected other options in location then in What computer Type show all option.

I have write a client script for it but its not working.

 

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

    // Get the 'What Computer Type?' field variable
    var computerTypeField = g_form.getControl('what_computer_type');
   

   
    // Clear out any previously selected values in 'What Computer Type?'
    //g_form.setValue('what_computer_type', '');

    //Check the selected value in the 'Location' field
    if (newValue == 'Richmond') {
        // Restrict to "Laptop" and "Desktop" options only
        g_form.setOption('what_computer_type', 'Laptop', true);   // Enable Laptop
        g_form.setOption('what_computer_type', 'Desktop', true);  // Enable Desktop
        g_form.setOption('what_computer_type', 'Docking Station', false);  // Disable Docking Station
    } else {
        // If location is not "Richmond", show all options
        g_form.setOption('what_computer_type', 'Laptop', true);  
        g_form.setOption('what_computer_type', 'Desktop', true);  
        g_form.setOption('what_computer_type', 'Docking Station', true);  

    }
 
}
 
What changes i have to do in above script?
Help me to get the desired output. Or is their any other way except scripting because in UI policy options for select box type variable is not showing.
1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @MayurChaudhari 

  • Please refer to this detailed solution:
  • Variables in catalog item is configured as:

Screenshot1: variable "Location"

JuhiPoddar_1-1732352645618.png

Screenshot2: variable What computer type?

 

JuhiPoddar_0-1732352611122.png

 

Client Script: onChange

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Clear all existing options
    g_form.clearOptions('what_computer_type');

    if (newValue == 'Richmond') {
        // Add specific choices for Richmond
        g_form.addOption('what_computer_type', 'Laptop', 'Laptop');
        g_form.addOption('what_computer_type', 'Desktop', 'Desktop');
    } else {
        // Add all options for other locations
        g_form.addOption('what_computer_type', 'Laptop', 'Laptop');
        g_form.addOption('what_computer_type', 'Desktop', 'Desktop');
        g_form.addOption('what_computer_type', 'Docking Station', 'Docking Station');
    }
}

Client script: onLoad

function onLoad() {
    //Type appropriate comment here, and begin script below
    var location = g_form.getValue('location');
    g_form.clearOptions('what_computer_type');
    if (location == 'Richmond') {
        // Add specific choices for Richmond
        g_form.addOption('what_computer_type', 'Laptop', 'Laptop');
        g_form.addOption('what_computer_type', 'Desktop', 'Desktop');
    }
}

Result:

JuhiPoddar_2-1732353113795.pngJuhiPoddar_3-1732353145717.png

Note: Please adjust the variable name and value in scripts

Hope this helps!

 

"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Do not use getControl or setOption for this, rather the supported and recommended clearOptions, addOption, and removeOption

https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/... 

Juhi Poddar
Kilo Patron

Hello @MayurChaudhari 

  • Please refer to this detailed solution:
  • Variables in catalog item is configured as:

Screenshot1: variable "Location"

JuhiPoddar_1-1732352645618.png

Screenshot2: variable What computer type?

 

JuhiPoddar_0-1732352611122.png

 

Client Script: onChange

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Clear all existing options
    g_form.clearOptions('what_computer_type');

    if (newValue == 'Richmond') {
        // Add specific choices for Richmond
        g_form.addOption('what_computer_type', 'Laptop', 'Laptop');
        g_form.addOption('what_computer_type', 'Desktop', 'Desktop');
    } else {
        // Add all options for other locations
        g_form.addOption('what_computer_type', 'Laptop', 'Laptop');
        g_form.addOption('what_computer_type', 'Desktop', 'Desktop');
        g_form.addOption('what_computer_type', 'Docking Station', 'Docking Station');
    }
}

Client script: onLoad

function onLoad() {
    //Type appropriate comment here, and begin script below
    var location = g_form.getValue('location');
    g_form.clearOptions('what_computer_type');
    if (location == 'Richmond') {
        // Add specific choices for Richmond
        g_form.addOption('what_computer_type', 'Laptop', 'Laptop');
        g_form.addOption('what_computer_type', 'Desktop', 'Desktop');
    }
}

Result:

JuhiPoddar_2-1732353113795.pngJuhiPoddar_3-1732353145717.png

Note: Please adjust the variable name and value in scripts

Hope this helps!

 

"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar