Multi-Row Variable Sets

Multi-row variable set is a new feature that has been introduced in the London release of ServiceNow. It is related to record producers and catalogue items.

Here are a few things I have learnt with regard to how they work.

What is a multi-row variable set (MRVS) and why/how would you use it?

What do you do when you need to create a catalogue item that would add multiple users to the system, wherein you would like to specify their name, position, role, and so on? Normally, you would create multiple catalogue items and add them to your cart.

But now, it is possible to do all of this in one catalogue item, without having to add multiple request items.

This will work with a pop up that will show up to add the row.

Once it has been added, it will appear in the list.

You can also use the ‘Edit’ button to edit your line.

Pressing the X icon will ask you to ‘confirm’ it, similar to the ‘Remove all’ button.

How to create a multi-row variable set (MRVS)?

You can go to the variable set or find it on the catalogue item or record producer form and create a new one there.

Once you are there, it will ask you if you want to have a single-row variable set or a multi-row one.

The single-row one is meant for the ‘old’ variable set structure, where you can add multiple fields that can be reused for different catalogue items.

Note: You can reuse the MRVS in other catalogue items as well.

Development and limitations

Client scripts
Works Limitation
OnLoad: You can add a setValue that will pre-set the correct data onSubmit: There is no possibility to add or verify something
onChange: You can add error messages onChange: You can’t verify the variable set data that has already been logged*

*You can add a script in the onChange or onSubmit client script in the catalogue item in order to verify the current data. However, you can’t do this ad-hoc when the user is logging it.

UI policies
Works Limitation
If all types of scripts execute true/false (except setVisible/setDisplay) Visibility or ‘not visible’ setting of a variable in a MRVS
Mandatory / read only / clear value in the catalogue UI policy actions  

Possibilities

You can use g_form.addInfoMessage and all other client scripts, except the one for changing the visibility of the fields on the form. Alternatively, you can also use mandatory and read-only values to configure it.

Limitations

At this point, it is not possible to hide a certain field on the ‘Add Row’ pop up, as it’s greyed out. Moreover, scripted items, such as g_form.setDisplay or g_form.setVisible, do not work in this case.

Catalogue client scripts
Works Limitations
Client scripts with an onLoad work when the MRVS is loaded You can’t hide fields with client scripts either
onChange client scripts can and do configure onSubmit does not work either in this regard, so you can’t stop the submission (you could try this in the normal catalogue item when you press onSubmit to call on faulty entries)

Useful scripts

The most useful scripts that can be executed to check the multi-row variable set (MRVS) are written in the catalogue item/record producer.

The procedure to check if a column in the MRVS has duplicate values is given below:

var mrvs = JSON.parse(g_form.getValue('user_mrvs');); //parse it to an object
var getAllUsers = mrvs.map(function(mrvs) { //create an array of the name
 return mrvs.add_user;
});
var sameUser = hasDuplicates(getAllUsers); //check if there are duplicates
if (sameUser) {
 alert('same user found, clean up your data');
}
function hasDuplicates(array) { //script to check if it has a duplicate
 return (new Set(array)).size !== array.length;
}

The way to check if you have duplicate lines in the MRVS is as follows:

var hasDuplicateLines = chkDuplicates(mrvs,true);
if(hasDuplicateLines){
alert('The MRVS has duplicate lines in the data set. Please verify before submission');
return false;
}
function chkDuplicates(arr,justCheck){
var len = arr.length, tmp = {}, arrtmp = arr.slice(), dupes = [];
arrtmp.sort();
while(len--){
var val = arrtmp[len];
if (/nul|nan|infini/i.test(String(val))){
 val = String(val);
}
if (tmp[JSON.stringify(val)]){
   if (justCheck) {return true;}
   dupes.push(val);
}
tmp[JSON.stringify(val)] = true;
}
return justCheck ? false : dupes.length ? dupes : null;
}

The method to get an array of a column from the MRVS has been provided below:

var mrvs = JSON.parse(g_form.getValue('add_multiple_fields'));
var getPrio = mrvs.map(function (mrvs) {
  return mrvs.priority;
});
alert('array of priority ' + getPrio);

The procedure to return an object that matches a condition in the MRVS is as follows:

var mrvs = JSON.parse(g_form.getValue('add_multiple_fields'));
var allP1 = mrvs.filter(function (mrvs) {
    return mrvs.priority ==1;
  });
alert('all P1 users' + JSON.stringify(allP1));

How to clear a multi-row variable set with a script

g_form.setValue(‘user_mrvs’, JSON.stringify([]));

About the Author

Levi Verbeke is an experienced ServiceNow architect, PM and BA with over eight years of ServiceNow architectural and development background. He has worked with various applications and integrations within ServiceNow. Moreover, he has worked on numerous ServiceNow projects for large multinational companies in Belgium, Germany, Netherlands, the UK and Switzerland.

Updates

Blog