Update value in Multi Row Variable Set (MRVS)
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]
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:
With the following settings:
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!:
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.
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:
https://www.servicenow.com/community/developer-articles/update-value-in-multi-row-variable-set-mrvs/ta-p/2324285