Article - In a Catalog Item : How to Fetch Main Form Variable in a (MRVS) in Service Portal

Shashank_Jain
Kilo Sage

This article explains how to access and use variables from the main Catalog Item form inside a Multi-Row Variable Set (MRVS), specifically within the Service Portal environment.

 

Use Case : In many ServiceNow catalog items, you might want to fetch a value from the main form and use it inside a Multi-Row Variable Set (MRVS) — for example, to carry over data like location, department, or user information into each MRVS row.

 

Why We Can't Use g_form Directly in MRVS to Access Main Form Variables :

1. Each Multi Row Variable Set has its own instance of g_form that cannot access the data in the main form.

2. Client scripts run in isolated scopes, so variables defined in one script aren’t visible in another.

 

How to Achieve This — A Simple Example

Let's understand this with a simple example:

 

We created a catalog item called "TEST CATALOG". In this catalog item, there is a variable called Short Description on the main form. We also created a Multi-Row Variable Set (MRVS) named "Test MRVS", and inside it, there is a variable called Description.

Our goal is: when the MRVS row loads, the Description field inside the MRVS should automatically get the value from the Short Description on the main form.

 

Steps to Achieve This :

 

STEP - 1 Create an On Load Client Script on the Catalog Item.

Make sure to set Isolate Script to false on the client script.

Please refer the below screenshot :

Screenshot (54).png

Use script : 

function onLoad() {
    //Type appropriate comment here, and begin script below
    this.my_g_form = g_form; // Make the main form's g_form available globally
}

 

- Why We Used this.my_g_form = g_form;

We did this to save the main form’s data access object (g_form) in a place (this.my_g_form) where other parts of the page can use it. This way, even the MRVS (which normally can’t see the main form) can get values from the main form. This makes my_g_form a property of the current script object, so it can be accessed from other scripts or places that share the same context.

 

- What is “Isolate Script” and Why Set It to False?

Isolate Script means each script runs separately and can’t share variables with other scripts.

When it’s true (active), the scripts can’t “talk” to each other.

We set Isolate Script to false so that our saved variable (this.my_g_form) can be seen and used by other scripts, like the MRVS script.

 

STEP : 2  Create an on load client script on variable set.

On Load Catalog Client Script inside MRVS to fetch a value from the main form and set it in the MRVS field.

Please refer the below screenshot :

Screenshot (55).png

 

Use script as per your variables :

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

 

Now, when we fill in the Short Description, it will automatically populate the MRVS field Description as expected.

Screenshot (56).png

 

 

Screenshot (57).png

 

This is how we can retrieve a value from the main form and use it inside the MRVS.

 

Note : 

You can practice this on your PDI. I’ve attached the update set named "Test - To set MRVS field value", which includes the following:

  • A catalog item called "TEST CATALOG" with one field: Short Description

  • An attached MRVS named "Test MRVS", which contains a field: Description

  • Two On Load Catalog Client Scripts – one on the catalog item and one on the MRVS

 

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

Nehal Dhuri
Mega Sage

It’s very helpful and definitely worth reading.

Thanks @Shashank_Jain 

Please hit like and mark my response as correct if that helps

Welcome @Nehal Dhuri , Glad you found it helpful 🙌

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