Update value in Multi Row Variable Set (MRVS) - ServiceNow Community
Willem
Giga Sage
Giga Sage

Hi there,

The following article is intended to help people to update values within a Multi Row Variable Set (MRVS) based on value(s) set on a variable on the catalog item.

I have written this for the following fictitious scenario. We want to manage salary via the Service Catalog.We want to set the Job title and Salary in the variable and then all rows that match within the MRVS must be updated.

 

We have a Multi Row Variable Set "variable_set_1" containing: Employee name[employee_name], Job title[employee_job_title] and Salary[employee_salary].

On the Catalog Item we have Job title[job_title] and Salary[salary]

find_real_file.png

If we fill in the Job Title and Salary, we want all Salary values to be updated for that Job title. So for example We fill in Developer as the job title and bump up the salary to 2000 the result must be 2000 for all Developers.

In order to do this we need to add a Catalog Client Script:

find_real_file.png

 

With the following settings:

find_real_file.png

The script:

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


    //get the MRVS
    var multiRowVariableSet = JSON.parse(g_form.getValue('variable_set_1'));

    //If you want to do something with the number of rows
    var numberOfRows = multiRowVariableSet.length;

    for (var i = 0; i < multiRowVariableSet.length; i++) {
        //check if the the job title matches
        if(multiRowVariableSet[i].employee_job_title == g_form.getValue("job_title")){
            //Update the value of a field with the new value
            multiRowVariableSet[i].employee_salary = newValue;
        }
    }
    
    //update the MRVS
    g_form.setValue('variable_set_1', JSON.stringify(multiRowVariableSet));
}

 

And of course the result!:

find_real_file.png

 

The script in more detail:

Retrieve the Variable Set:

    //get the MRVS
    var multiRowVariableSet = JSON.parse(g_form.getValue('variable_set_1'));

This is an array of Objects. Each object containing the fields and their values.

find_real_file.png

 

Get the number of Rows in the MRVS:

We can check how many Rows we have by requesting the length of the "multiRowVariableSet"-array.

var numberOfRows = multiRowVariableSet.length;

 

Get the value of a variable within the MRVS:

As we have seen each Object contains the fields that we can access and we do that in our check:

if(multiRowVariableSet[i].employee_job_title == g_form.getValue("job_title"))

We compare the value of the multi row variable set "employee_job_title"-field against the one on the main Catalog Item "job_title".

 

Set value of a variable within the MRVS:

You can just as easily set the value of an item:

multiRowVariableSet[i].employee_salary = newValue;

 

Update the Multi Row Variable Set:

Up until now the values we have changed are only in the Objects that are in our "multiRowVariableSet"-array. We want to update the multi row variable set so that will have the values:

//update the MRVS
    g_form.setValue('variable_set_1', JSON.stringify(multiRowVariableSet));

 

Other articles on Multi Row Variable sets:

Multi Row Variable Set: Clear/Remove all values

Multi Row Variable Set: Limit Rows

Comments
shohebshaikh
Giga Expert

Hi Willem,

How do we do it if we are getting/changing any value from MRVS variable and updating it in the Catalog item Variable?

 

Regards,

Shoheb

Willem
Giga Sage
Giga Sage

The other way around can be done as well. So from the MRVS update the Catalog Item variable:

If we change the employee_salary we want to update the salary on the catalog item:

 

On the Multi Row Variable Set use the following on an onChange script on the Variable you want as the trigger. In my case the employee_salary:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	if(parent.g_form){
        parent.g_form.setValue('salary', newValue);
    }
}

 

AbhishekGardade
Giga Sage

Thanks and Appreciated!!! Very Explanatory Blog... 

Robby Lieuw
Tera Explorer

Thanks and keep up the good work!

Zorawar Singh
Kilo Contributor

Really Helpful details around here Willem. Just one thing I wanted to ask is if you have tried out the same thing on service portal to set a trigger on the main form onchange of the variable in MRVS. It works perfectly on the dekstop view but doesn't work on Service Portal. 

 

Laukik Udpikar
Tera Expert

Is there a way to check the MVRS for duplicate records and abort adding the duplicate record.

Babu Chokka1
Mega Contributor

Thanks, very useful and it worked for me.

MarioP1
Tera Contributor

This seem to only work when 'Try It' button clicked on Catalog Item. It does not work on Service Portal.

Any suggestions or ideas on how to achieve this on Service Portal?

I have a requirement to auto-populate data from MRVS to multi-line text on front end catalog item dynamically before submitting form.

For example:  Lets say MRVS contains following:

Now, the requirement is to auto-populate above values to 'Multi Line Text' field below:

Thanks

Willem
Giga Sage
Giga Sage

Can you share your script? Have you checked UI Type is set to 'All'?

Kanika4
Tera Contributor

Hi, I have a requirement where I have to restrict the variable in the multi-row variable set based on the value selected for the variable on the catalog item.

Example: Email is a multi-row variable and Group is a catalog item variable. if group is selected as gmail then the user should only allow to give the email_id that ends with domain = gmail.com.

Domain is a field that can be fetched by dot-walking through the groups table and user should only allowed to enter the domain of the selected group. e.g. groups.x.domain

Tarish
Kilo Contributor

Hi, I am using a onsubmit client script to calculate and add row to the multirow variable set. 

What happens is- just before submission I am able to see this row named Total , but just after submission this row disappears. 

i am using this ONSUBMIT Catalog client script, Can anyone have a look at this? 

 

var value = g_form.getValue('mrvs_name'); 

var parser = JSON.parse(value);

var sum = 0;

for(var i=0;i<parser.length;i++){

sum = sum + parseFloat(parser[i].quantity); // give here variable name inside MRVS

}


var obj =(g_form.getValue('mrvs_name').length!=0)? JSON.parse(g_form.getValue('mrvs_name')): [];
obj.push({

action:"",
rejection_reason: 7, //name of variables inside mrvs
quantity:sum 
});
g_form.setValue('mrvs_name',JSON.stringify(obj));

}

Community Alums
Not applicable

When applying a similar script, it looks like the MRVS is updating as expected on the client, but when I select the 'order' button, it is giving me an error: 'Something went wrong and your request could not be submitted.  Please contact your systems administrator'.    I'm using the following:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var multiRowVariableSet = JSON.parse(g_form.getValue('landline_variable_set'));

var numberOfRows = multiRowVariableSet.length;

for (var i=0; i < numberOfRows; i++) {
multiRowVariableSet[i].item = i+1;
// confirm(i);
}

g_form.setValue('landline_variable_set', JSON.stringify(multiRowVariableSet));

//Type appropriate comment here, and begin script below

}

 

Any thoughts are appreciated - thanks!

Nagashree5
Tera Contributor

Hi @Willem ,

Thanks so much for the article, I have a requirement like to auto-populate a read-only field inside a multi row variable set. The fields (Data Disk Number) value is the row number in the multi row variable set. So, as soon as the user inserts a new entry in the multi-row variable set, the Data Disk Number is populated with the row number. (E.g. 1,2,3,4...). Can this be achieved. Can you please guide me on this. 

Thank you in Advance.

jmiskey
Kilo Sage

That code is pretty cool, but I am having issues doing something somewhat similar.  I am trying to update existing MRVS records found in a RITM from a Catalog Item from a Run Script action in the Workflow that runs against that Catalog Item.  But I cannot get it to work. 

 

I have seemingly been able to identify/isolate the MRVS record that I need to update, just not so sure how to update in the Run Script action.  I have posted the details about it here: https://www.servicenow.com/community/developer-forum/update-existing-mrvs-record-row-from-workflow-s...

 

If anyone could provide any assistance, it would be greatly appreciated!

 

Thanks

Erik Stuer
Tera Guru

How would you change this if you want to set the value of a variable within the MRVS after the user selects an option in another variable (reference type) within the same MRVS? I have an item number linked to each product on a custom table. The user selects the product (first variable in the MRVS) and I want the item number of that record to populate in the second variable before hitting 'add' to finalized that row. 

Version history
Last update:
‎08-13-2020 11:45 AM
Updated by: