Client Script in a Catalog Item

Alexandre5
Giga Contributor

Hello guys, how are you?

I'm trying to create by myself a catalog item just for "practicing". I create a simple catalog item like "New Employee Form".

There are fields named "Position" that has three options (Manager - Analyst - Developer) and other filed named "Equipment" that has two options (Laptop - Desktop).

I would to when I select "Manager" position, just appear "Laptop" as equipment to select for ex. So, how can I do that? Can you help me? Thank you!

3 REPLIES 3

Voona Rohila
Kilo Patron
Kilo Patron

Hi Alexandre

Refer below article for adding dependents between 2 choices using catalog client script

https://community.servicenow.com/community?id=community_article&sys_id=ed94c724db75c410feb1a851ca961...

Try and let us know if you're stuck.

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Alexandre

There are many different way to do this.

Since just starting, let's use "Select Box" for both choices.

1. Variable definition

1.1 Position

find_real_file.png

1.2 Equipment

find_real_file.png

 

Method 1. Use UI Policy. If just displaying/hiding fields, UI Policy can be used.

https://docs.servicenow.com/en-US/bundle/sandiego-platform-administration/page/administer/form-admin...

Define UI Policy condition as "position" "is" "Manager" so the policy will execute when the position is Manager.

find_real_file.png

Then in the script, write a script to add/remove "desktop" option from equipment field.

find_real_file.png

Execute when true

function onCondition() {
  g_form.removeOption('equipment', 'desktop');
}

Execute if false

function onCondition() {
  g_form.addOption('equipment', 'desktop', 'Desktop');
}

 

Execution results

case when manager

find_real_file.png

case when not manager

find_real_file.png

Hitoshi Ozawa
Giga Sage
Giga Sage

This can also be done creating Catalog Client Script. The result is the same as above.

Set Type to "onChange"

Variable name to "position"

Script

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;
    }
    if (newValue == 'manager') {
        g_form.removeOption('equipment', 'desktop');
    } else {
        g_form.addOption('equipment', 'desktop', 'Desktop');
    }
}

find_real_file.png