How to make variable in MRVS hide/visible based on dropdown variable outside of MRVS in catalog item

nagakarthik0721
Tera Contributor

I have requirement to make a variable in MRVS to be hidden based on a selection in a drop down field which is outside of MRVS in a catalog item. How can I achieve this?

8 REPLIES 8

@nagakarthik0721 

In MRVS , you cannot select any variable outside it , i.e. catalog item.

 

This client script will hide the MRVS variable based on previously selected catalog  item variable.

 

For the functionality you are trying to achieve, you will have to create the choice variable in MRVS itself.


Raghav
MVP 2023

Shashank_Jain
Kilo Sage

@nagakarthik0721 ,

 

There is no way to access outside variable within MRVS script.

Go through these links it might help :

https://www.servicenow.com/community/developer-articles/multi-row-variable-sets-how-to-get-or-set-va...

https://www.servicenow.com/community/developer-forum/accessing-other-form-variables-from-inside-mult...

 

Or try implementing this solution : 

Sometimes, you need to share values between variables in the main form and a Multi-Row Variable Set (MRVS).

Each MRVS has its own instance of g_form that cannot directly access data in the main form. This restriction can be overcome by making the main g_form instance available to the MRVS. The implementation differs slightly between the backend and the Service Portal.

 

Backend Implementation

  • The MRVS is opened in a new iFrame.

  • Accessing the main g_form only requires using the parent variable.

  • Example: Create an onLoad Catalog Client Script on the MRVS to get a value from a variable in the main form and set it in the new row:

if (parent.g_form) {
    g_form.setValue('to', parent.g_form.getValue('from'));
}

 

Service Portal Implementation

In the Service Portal, the MRVS popup is not opened in an iFrame, so achieving the same requires two steps:

1. Make the main g_form available

  • Create an onLoad Catalog Client Script in the Catalog Item.

  • Set the main g_form as a global variable:

this.my_g_form = g_form; //Note: Set Isolate Script to false.

 

2. Access g_form from the MRVS

  • Create an onLoad Catalog Client Script in the MRVS to access the global variable:

if (this.my_g_form) {
    g_form.setValue('to', this.my_g_form.getValue('from'));
}

 

This will definitely work, if you have any doubt please feel free to ask!

 

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

Anand Kumar P
Giga Patron
Giga Patron

Hi @nagakarthik0721 ,

 

function onLoad() {
var dropdown = g_service_catalog.parent.getValue("Variable on the catalog item"); //replace with variable on the catalog item
if(dropdown==‘dropdownValue’){
g_form.setVisible("Variable on the MRVS", false ); //replace with variable on MRVS
}
}

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

@Anand Kumar P , 

I need the field in MRVS to be hidden whenever change happened to dropdown field outside MRVS. Please suggest.