logo

NJP

Create a dependency between two fields on a Catalog Item - Lookup multiple choice, Lookup select box, List Collector

Import · Jan 20, 2020 · article

Every so often the question comes up "How can I make one drop down dependent on the value in another?" and, while there are several ways to accomplish this, the easiest one is using a feature built into the platform.

For this process to work the values that you want to populate in both fields should be drawn from a table (can be different tables as long as there is a value to key on) with both variables being Lookup Select Boxes. In this example we'll use the cmn_location table.

Setup your first Lookup Select Box so that the values displayed are the ones you want the user to be able to select from.

For the example we'll name our first variable state and configure the Lookup Select Box to lookup values form the cmn_location table, from the field "State / Province". We'll include the option for none and select unique values only.

For the second Lookup Select Box (call it country) we will also lookup values from the cmn_location table, from the field "Country". We won't include none for this field, but we will select unique values again. In addition, we will need to set a reference qualifer on the field (same tab as the type specifications)

javascript: 'state='+current.variables.state

We then need to add a variable attribute (same tab as default value) which would look like this:

ref_qual_elements=state

When you load the item and select a state, the value displayed in country will automatically be updated to reflect the country associated with the selected state.

What makes this possible is in part the reference qualifier, which is written to reference the current value of the state field (but, if you notice, this is written in server-side, not client-side javascript).

The second things that makes this possible is the variable attribute that we configured - ref_qual_elements=state. This attribute is a flag that tells the platform to monitor the field you name for changes and, if detected, re-evaluate the reference qualifier and displayed values server-side.

Note, this same process can be applied to Lookup multiple choice and List Collector type fields as well as Lookup select box. You are also not limited to only one dependent field. You can specify multiple fields to watch in ref_qual_elements by separating variable names with a semi-colon such as state;country;street

If you found this article helpful, please be kind and remember to click appropriately. If you found it really helpful, maybe bookmark it for later reference.

View original source

https://www.servicenow.com/community/developer-articles/create-a-dependency-between-two-fields-on-a-catalog-item-lookup/ta-p/2300301