Setting defaults in a Multi-Row Variable Set
You can use g_form.setValue to set values in a multi-row variable set by passing an array that contains key/value pairs that reflect your multi-row variable set variables. For example, here's a multi-row variable set called travellers that has variables traveller, mobile_phone, additional_information
g_form.setValue('travellers',[{"additional_information":"","mobile_phone":"12345","traveller":"Peter"}]);
The challenge comes if you have any variables that use a reference field. As you can only pass one string for each variable, either the value (sys_id) or the display value (in this case, "Peter"). The problem is...
- If you pass the sys_id then the sys_id will appear on the screen (not good) but when you edit the line, it'll resolve to the display value
- If you pass the display value, then it appears on the screen okay, but if you edit the line it won't resolve at all
Others may have simpler ways to do this, but I used a bit of angular magic and set the display value normally (just like in the example above), while using angular to set the sys_id in the background.
NOTE: This is the onLoad script for the multi-row variable set, not the catalog item itself
function onLoad() {
var multiRowVariableSetName = 'travellers';
//get the window object by stealth
var myWindow = (0,eval)('this');
//get the service portal angular object already loaded into memory (instead of making an ajax call).
var ngScope = myWindow.angular.element(myWindow.document).scope();
var ngMain = myWindow.angular.element('main');
//find the variable set by name (MUST be unique)
var findMRVS = ngMain.injector().get('$filter')('filter')(ngMain.scope().containers,{$:multiRowVariableSetName});
//set up an object pointing at the multi-row variable set scope data
var thisMRVS = findMRVS[0].rows[0].columns[0].widgets[0].widget.data.sc_cat_item._fields[multiRowVariableSetName];
//these names must match the names of variables in the variable set
var thisValue = [{"additional_information":"","mobile_phone":ngScope.user.mobile_phone,"traveller":ngScope.user.sys_id}];
//First, use a regular setValue (but this won't set the sys_id)
g_form.setValue('travellers',[{"additional_information":"","mobile_phone":ngScope.user.mobile_phone,"traveller":ngScope.user.name}]);
//Second, set the sys_ids
thisMRVS.value = JSON.stringify(thisValue);
thisMRVS.stagedValue = JSON.stringify(thisValue);
//Optional: Write the angular object for this multi-row variable set to the console log
//console.log(thisMRVS);
//console.log(ngScope);
}
As with anything bespoke like this, YMMV, but for me this worked really well.
If you look at the angular object (thisMRVS) you'll see there settings like max_rows_size which can (probably) be used to restrict how many rows someone can enter.
To adapt this to work with your variable set, you'll need to alter the values of the variables multiRowVariableSetName and thisValue so they reflect your multi-row variable set.
Have fun
https://www.servicenow.com/community/now-platform-articles/setting-defaults-in-a-multi-row-variable-set/ta-p/2320776