How to hide a variable set based on a catalog item variable value?

Bidduam
Tera Guru

I have a variable set that I've added to my catalog item, but I only want it to be shown if a particular catalog item variable is set to no.

The variable set is called:

vs_address_details

It's sys_id is:

df104b8ec1a25250daa3b371506bdab9

 

The variable (that is not in the variable set) on the catalog item is a yes/no field:

u_office_pickup_yn

 

I have tried a catalog ui policy, but it won't work if I set it to catalog item and choose 'visible' and "Reverse if false" is set to true on each field in the variable set, or even just choose the variable set.

 

Also if I try it on the actual variable set, I can't choose the variable on the catalog item as a condition.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Bidduam 

another way is to explicitly show/hide the variables within the variable set based on the other variable

remember you need to make the variables non-mandatory to hide them

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

@Bidduam 

I believe the UI policy won't work as there might be some individual UI policies on each of the individual variables

Did you use onChange catalog client script which Applies to Catalog Item and that variable and then show/hide the entire variable set using this logic?

g_form.setDisplay('VARIABLE_SET_NAME', false); // hide

g_form.setDisplay('VARIABLE_SET_NAME', true); // show

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

pavankenche
Tera Contributor

To achieve this, you need to use a Catalog Client Script instead of a Catalog UI Policy. Catalog UI Policies do not support conditions based on catalog item variables when the variable is not part of the same variable set. However, you can write a simple onChange Catalog Client Script to dynamically show or hide the fields within the variable set based on the value of your catalog item variable (u_office_pickup_yn).

 

Steps to Achieve This Solution:

1. Get the Variable Names Inside the Variable Set

Since you can't directly target the entire Variable Set, you will need to list all the variables within the vs_address_details variable set. You can find the list of variables by navigating to the Variable Set and noting down the names (under the Variables related list).

Example:

var addressDetailsVars = ['address_line1', 'city', 'zipcode']; // Replace with your actual variable names

2. Create a Catalog Client Script for the u_office_pickup_yn Variable

  • Navigate to your Catalog Item.

  • Scroll down to Catalog Client Scripts → Click New.

  • Configure the script like this:

Field Value

NameShow/Hide Address Details
TypeonChange
Variable Nameu_office_pickup_yn
UI TypeAll (or Desktop/Mobile as required)

 

3. Write the Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;

// List of variables in the vs_address_details variable set
var addressDetailsVars = ['address_line1', 'city', 'zipcode']; // Replace with your variable names

// Show or hide the address fields based on the value of u_office_pickup_yn
var showAddressFields = (newValue === 'no'); // Show if 'No' selected

// Loop through the variables and set visibility
for (var i = 0; i < addressDetailsVars.length; i++) {
g_form.setDisplay(addressDetailsVars[i], showAddressFields);
}
}

4. (Optional) Add an onLoad Script to Hide Fields Initially

If you want to ensure the fields are hidden when the form initially loads (if u_office_pickup_yn is "Yes" by default), add the following onLoad Catalog Client Script:

function onLoad() {
  var addressDetailsVars = ['address_line1', 'city', 'zipcode']; // Replace with your variable names

  // Get the value of the u_office_pickup_yn field
  var pickup = g_form.getValue('u_office_pickup_yn');
  
  // Show or hide the fields based on the value
  var showAddressFields = (pickup === 'no');

  // Loop through the variables and set visibility
  for (var i = 0; i < addressDetailsVars.length; i++) {
    g_form.setDisplay(addressDetailsVars[i], showAddressFields);
  }
}

Conclusion:

  • Catalog UI Policies will not work across different containers like Variable Sets and Catalog Item variables. Instead, you can use Catalog Client Scripts.

  • Use the onChange script to dynamically show or hide variables inside a variable set based on the value of a catalog item variable (u_office_pickup_yn).

  • Optionally, use an onLoad script to hide the fields initially when the form loads.

By using the above approach, you can easily achieve conditional visibility for a variable set based on a catalog item variable.


If you found this solution helpful, please mark it as helpful!

Let me know if you have further questions or need clarification!

pavankenche
Tera Contributor

To achieve this, you need to use a Catalog Client Script instead of a Catalog UI Policy. Catalog UI Policies do not support conditions based on catalog item variables when the variable is not part of the same variable set. However, you can write a simple onChange Catalog Client Script to dynamically show or hide the fields within the variable set based on the value of your catalog item variable (u_office_pickup_yn).

 

1. Create a Catalog Client Script for the u_office_pickup_yn Variable

Field Value

NameShow/Hide Address Details
TypeonChange
Variable Nameu_office_pickup_yn
UI TypeAll (or Desktop/Mobile as required)

 

2. Write the Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;

// List of variables in the vs_address_details variable set
var addressDetailsVars = ['address_line1', 'city', 'zipcode']; // Replace with your variable names

// Show or hide the address fields based on the value of u_office_pickup_yn
var showAddressFields = (newValue === 'no'); // Show if 'No' selected

// Loop through the variables and set visibility
for (var i = 0; i < addressDetailsVars.length; i++) {
g_form.setDisplay(addressDetailsVars[i], showAddressFields);
}
}

 

Conclusion:

  • Catalog UI Policies will not work across different containers like Variable Sets and Catalog Item variables. Instead, you can use Catalog Client Scripts.

  • Use the onChange script to dynamically show or hide variables inside a variable set based on the value of a catalog item variable (u_office_pickup_yn).

  • Optionally, use an onLoad script to hide the fields initially when the form loads.

By using the above approach, you can easily achieve conditional visibility for a variable set based on a catalog item variable.


If you found this solution helpful, please mark it as helpful!

Let me know if you have further questions or need clarification!

pavankenche
Tera Contributor

To achieve this, you need to use a Catalog Client Script instead of a Catalog UI Policy. Catalog UI Policies do not support conditions based on catalog item variables when the variable is not part of the same variable set. However, you can write a simple onChange Catalog Client Script to dynamically show or hide the fields within the variable set based on the value of your catalog item variable (u_office_pickup_yn).

 

2. Write the Script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;

// List of variables in the vs_address_details variable set
var addressDetailsVars = ['address_line1', 'city', 'zipcode']; // Replace with your variable names

// Show or hide the address fields based on the value of u_office_pickup_yn
var showAddressFields = (newValue === 'no'); // Show if 'No' selected

// Loop through the variables and set visibility
for (var i = 0; i < addressDetailsVars.length; i++) {
g_form.setDisplay(addressDetailsVars[i], showAddressFields);
}
}

 

 

Conclusion:

  • Catalog UI Policies will not work across different containers like Variable Sets and Catalog Item variables. Instead, you can use Catalog Client Scripts.

  • Use the onChange script to dynamically show or hide variables inside a variable set based on the value of a catalog item variable (u_office_pickup_yn).

  • Optionally, use an onLoad script to hide the fields initially when the form loads.

If you found this solution helpful, please mark it as helpful!

Let me know if you have further questions or need clarification!