Populate Multi row variable set using script

Alon Grod
Tera Expert

Hi,

 

i have a MRVS on my catalog item with two variables of type Reference. In addition, i have a variable of type reference on my catalog item itself. How can I populate one of the variables in the MRVS with the value of the variable in my catalog item. Both the variable from the MRVS and the variable from the catalog item are referenced to the same table

1 ACCEPTED SOLUTION

RaghavSh
Kilo Patron

This should be quite simple considering you will populate your catalog item variable before  MRVS.

 var user1 = g_service_catalog.parent.getValue("user");  // user is. variable in catalog item
 g_form.setValue('user_mrvs', user1);  // user_mrvs is varaiable on multi row variable set

** this is on load client script on MRVS**
**Note : The on Load client script works on MRVS when you click on Add button**

Raghav
MVP 2023

View solution in original post

5 REPLIES 5

Bhuvan
Kilo Patron

@Alon Grod 

 

You can refer below posts that are similar to your requirement,

 

https://www.servicenow.com/community/itsm-forum/how-to-populate-requestor-details-in-variables-of-mr...

 

https://www.servicenow.com/community/developer-forum/how-to-auto-populate-multi-row-variable-set/m-p...

 

If this helped to answer your query, please accept the solution and close the thread.

 

Thanks,

Bhuvan

RaghavSh
Kilo Patron

This should be quite simple considering you will populate your catalog item variable before  MRVS.

 var user1 = g_service_catalog.parent.getValue("user");  // user is. variable in catalog item
 g_form.setValue('user_mrvs', user1);  // user_mrvs is varaiable on multi row variable set

** this is on load client script on MRVS**
**Note : The on Load client script works on MRVS when you click on Add button**

Raghav
MVP 2023

@Alon Grod Did this work as expected 


Raghav
MVP 2023

Rafael Batistot
Tera Sage

Hi @Alon Grod 

 

May you use script onLoad 

 

function onLoad() {
// get value from catalog item variable
var catVar = g_form.getValue('u_catalog_ref');

// set that value into the MRVS first row (if it exists)
var mrvs = g_form.getValue('u_mrvs_variable');
if (mrvs) {
var rows = JSON.parse(mrvs);

if (rows.length > 0) {
rows[0].u_mrvs_ref = catVar; // copy value into MRVS reference variable
g_form.setValue('u_mrvs_variable', JSON.stringify(rows));
}
}
}