logo

NJP

Few Scenarios on MultiRowVariableset(MRVS) for Catalog and Record Producer

Import · Jan 07, 2022 · article

Hello Everyone,

In this Article we will have a look on few scenarios in MRVS.

1) How to restrict the user cannot enter more than N rows?

Lets say for example we need to restrict the user to enter only 3 rows in MRVS.

So, We can add this logic in Variable Set attributes:

max_rows=n //where 'n' is the number of rows

image

In above screenshot setting the max_rows=3 in variable set attributes, so after adding 3 rows in MRVS Add button will be disabled.

2) How to restrict the user cannot enter the same value in variable?

Lets say, We have a reference field referring to user table. Selected Fred Luddy in 1st row, now we need to restrict the user cannot select the same user again(Fred Luddy). For this we have a Unique checkbox available in the Variable form.

image

I am added Fred Luddy in 1strow, Now if I try to add the same user again it will show the error like below screenshot so user cannot Add the row with Fred Luddy as a user again.

image

Refer this docs for more info on the Unique checkbox.

3) How to add the Logged-in user details in MRVS based on the check box?

We can do this using onChange() Client script and Script include:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getBooleanValue('u_addme') == true) {

        var ajax = new GlideAjax('Newglidingabc');
        ajax.addParam('sysparm_name', 'addRowinMRVS');
        ajax.addParam('sysparm_existing', g_form.getValue('user_details')); //MRVS data
        ajax.getXML(getRITMdata);

        function getRITMdata(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue('user_details', answer);
        }
    } else {
        var aja = new GlideAjax('Newglidingabc');
        aja.addParam('sysparm_name', 'deleteRowinMRVS');
        aja.addParam('sysparm_existingdata', g_form.getValue('user_details'));//MRVS data
        aja.getXML(getRITMdat);

        function getRITMdat(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            g_form.setValue('user_details', answer);
        }
    }}
   addRowinMRVS: function() {
        if (this.getParameter('sysparm_existing') != "") {              //if multi-row varibleset is not empty
            var existingdata = this.getParameter('sysparm_existing');  //getting the existing details
            var existingdataStr = JSON.parse(existingdata);            //parsing the data
            var mrvsObj = [{
                "user_name": gs.getUserID(),
                "department": "IT",
                "email": "[email protected]"
            }];                                                       //logged-in user details
            var finalObj = mrvsObj.concat(existingdataStr);          //combining both using concat
            return JSON.stringify(finalObj);                        //returning into client side
        } else {                                                   //if multirow varibleset is  empty
            mrvsObj = [{
                "user_name": gs.getUserID(),
                "department": "IT",
                "email": "[email protected]"
            }];                                                  //logged-in user details
            return JSON.stringify(mrvsObj);                     // directly returning into client side
        }
    },
deleteRowinMRVS: function() {
        var existingdataStr = JSON.parse(this.getParameter('sysparm_existingdata'));      //parsing the data
        for (var i = 0; i < existingdataStr.length; i++) {                                //checking for all rows
            if (existingdataStr[i].user_name == "6816f79cc0a8016401c5a33be04be441") {     //checking if logged-in user exists
                existingdataStr.splice(i, 1);                                             //removing the row using splice
            }
        }
        return JSON.stringify(existingdataStr);                                          //returning into client side
    },

Have a look on the below image:

image

Have a look on this videos for more info. on JSON format.

4)How to send the approvals to managers based on the users selected?

In Advanced script section in Approval User activity we can write the code like below:

answer = [];
var mrvs = current.variables.user_details;      //use MRVS internal name
for (var i = 0; i < mrvs.getRowCount(); i++) {
    var row = mrvs.getRow(i);                  // getting the row one by one using getRow
    var grU = new GlideRecord('sys_user');
    if (grU.get(row.user_name)) {
        answer.push(grU.getValue('manager')); //sending approval to manager
    }
}

5)How to send MRVS data in Notification?

We can achieve this using mail script and we can call the same in Notification:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var mrvs = current.variables.user_details; //use MRVS internal name
    var count = mrvs.getRowCount(); //gives the row count of the MRVS
    if (count) { //checking if it has atleast one row
        template.print('<table border=1>'); //open the table
        template.print('<thead>');
        template.print('<tr>');
        template.print('<th colspan="4"> MultiRow Variable set data</th>'); //column span
        template.print('</tr>');
        template.print('</thead>');
        template.print('<tr>');
        template.print('<th> S.No </th>');
        template.print('<th> Name</th>');
        template.print('<th> Email </th>');
        template.print('<th> Department </th>');
        template.print('</tr>');
        for (var i = 0; i < count; i++) {
            var row = mrvs.getRow(i);
            template.print('<tr>');
            template.print('<td>' + (i + 1 + ".") + '</td>');
            var gUser = new GlideRecord('sys_user');
            if (gUser.get(row.user_name))
                template.print('<td>' + gUser.getDisplayValue() + '</td>');
            template.print('<td>' + row.email + '</td>');
            var gDept = new GlideRecord('cmn_department');
            if (gDept.get(row.department))
                template.print('</td>' + gDept.getDisplayValue() + '</td>');
            template.print('</tr>');
        }
        template.print('</table>'); //close the table
    } else {
        template.print('No data in MRVS'); //if MRVS is empty
    }
})(current, template, email, email_action, event);

Output will be like below screenshot:-

image

I've learnt table structure format from Codecademy

6) Mapping the MRVS data into form fields in Record producer?

Refer this Thread for reference.

7) Adding row in MRVS dynamically in server side?

Refer this Thread for reference.

Hope it helps

Thanks,

Murthy

<Previous Article Next Article>

View original source

https://www.servicenow.com/community/itsm-articles/few-scenarios-on-multirowvariableset-mrvs-for-catalog-and-record/ta-p/2300716