logo

NJP

Bulk Upload — MRVS

Import · Apr 05, 2023 · article

There is a common requirement where the feature is provided to requesters where they can submit a single request with bulk information. The real-time scenarios can be: Group membership for multiple users, creating multiple customer contacts, reimbursement requests.

Although the MRVS serves the similar purpose where a user can add the multiple rows in MRVS by clicking on Add button. But sometimes it is quite time taking if the MRVS has multiple variables in it.

So, if there anything where the user will upload an excel with n number of rows and the same excel is transformed into a MRVS?

Yes, we will require some minor configurations and customization to achieve this by following the steps below:

I am using the example of uploading User Data(Name and Email)

  1. Create the MRVS with the 2 variables.

2. Create a new variable of Type “Attachment”

3. Create a new client callable Script Include to parse the sheet and return data. You can read more about GlideExcelParser here

var AttachmentUtilsDN = Class.create();

AttachmentUtilsDN.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getXLSData: function() {  
    var arrObj = [];  
    var file_id = this.getParameter('sysparam_attSysId');  
    var row = '';  
    var att = new GlideSysAttachment().getContentStream(file_id);  
    var parser = new sn_impex.GlideExcelParser();  
    parser.parse(att);  
    var headers = parser.getColumnHeaders();  
    while (parser.next()) {  
        var obj = {};  
        row = parser.getRow();  
        obj.employee_name = row[headers[0]];  

obj.email = row[headers[1]];

arrObj.push(obj);

}

return JSON.stringify(arrObj);

},

type: 'AttachmentUtilsDN'  

});

4. On change of the Attachment variable write a script to call the script include and its done. Make sure the excel column matches the backend name of the field.

Sample Excel

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

try {

parseXLS();

} catch (e) {

console.log("Exception caugght in " + e.message);

}

function parseXLS() {  
    var att = new GlideAjax('AttachmentUtilsDN');  
    att.addParam('sysparm_name', 'getXLSData');  
    att.addParam('sysparam_attSysId', newValue);  
    att.getXML(excelParser);  

    function excelParser(response) {  
        var answer = response.responseXML.documentElement.getAttribute('answer');  
        g_form.setValue('employees', answer);  
    }  
}  

}

This use case is quite minimal, but you can always manipulate the returned data and apply validations. For example, based on the Email entered you can query the sys_user table to get the location and populate in the MRVS dynamically.

Demo

View original source

https://deepakvednegi.medium.com/bulk-upload-mrvs-f48ab544870f