logo

NJP

Set defaults in a multi-row variable set

Import · Jun 30, 2019 · article

If you want to set the defaults in a multi-row variable set at the point the main catalog item or record producer loads, you can do that with this script...

In this example, the multi-row variable set is called travellers while each individual entry has traveller, mobile phone and additional information, so you'll need to alter your code to suit your multi-row variable set.

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.travellers;

    //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);
}
View original source

https://www.servicenow.com/community/now-platform-articles/set-defaults-in-a-multi-row-variable-set/ta-p/2310586