Export multi-row variable set to excel or csv?

Brun
Mega Expert

Hi Experts,

 

I'm began using some MRVS within an item, and find that it's really difficult to read from the task or RITM form due to the amount of columns and rows getting added. Has anyone been able to export a MRVS to an excel file or csv and attach it to the RITM? 

If not, is there any way to make the table easier to read for the fulfiller? Even exporting to a PDF would be beneficial.

 

Thank you!

12 REPLIES 12

TJ29
Tera Contributor

Hi @Feasood,

 

Please see below that works for my environment, you'll want to focus online 14 to pass in your variables, making sure they match the ordering of your labels/headers.

 

var mrvsexp = [];
var mrvs = current.variables.ad_group_information_dev; //replace with the internal name of your MRVS
var ritm = current.request_item.number;
var rowCount = mrvs.getRowCount();
mrvsexp.push('Samaccountname,Description,Owner,Members,Memberof,Varonis,Access Management,Info\r\n'); //Column Headers - replace with your variable labels
for (var i = 0; i < rowCount; i++) {
    var row = mrvs.getRow(i);

    var usr = new GlideRecord('sys_user');
    usr.addQuery('sys_id', row.owners);
    usr.query();
    usr.next(); {
        mrvsexp.push(row.d_ad_group_name_confirmation + ',' + row.d_description + ',' + row.d_owners_usernames + ',' + row.d_members_combined + ',' + row.d_member_of_converted + ',' + row.d_varonis_ad_group + ',' + row.d_access_management + ',' + row.d_owners_usernames + " are the owners of this group. This group was created as part of " + ritm + '\r\n');
    }
}

var attachment = new Attachment();
var attachmentRec = attachment.write('sc_task', current.sys_id, 'DEV export.csv', 'text/csv', mrvsexp.join(''));

var att = new GlideRecord('sys_attachment');
att.addQuery('file_name', 'DEV export.csv');
att.addQuery('table_sys_id', current.sys_id);
att.orderByDesc('sys_created_on');
att.query();
if (att.next()) {
    var snEnv = gs.getProperty('instance_name');
    var attsysid = att.sys_id.toString();
    var URL = "https://" + snEnv + ".service-now.com/sys_attachment.do?sysparm_referring_url=tear_off&view=true&sys_id=" + attsysid;
    current.update();
    action.setRedirectURL(URL);
} else {
    current.update();
    action.setRedirectURL(current);
}

Thanks TJ,

 

I have used your script and altered it so that it included my Column headings and variable names etc, but I still get an empty spreadsheet with only headings.  I have attached screenshots of my test script and setup.  I've tried naming the variables as below and also as mrvsexp.push(row.d_name + ',' + row.d_address + ',' + row.d_phone_number + '\r\n');

 

Feasood_0-1722460478301.png

Feasood_0-1722477300892.png

 

 

 

var mrvsexp = [];
var mrvs = current.variables.leasing_vs; //replace with the internal name of your MRVS
var ritm = current.request_item.number;
var rowCount = mrvs.getRowCount();
mrvsexp.push('Name,Address,Phone Number\r\n'); //Column Headers - replace with your variable labels
for (var i = 0; i < rowCount; i++) {
    var row = mrvs.getRow(i);

    var usr = new GlideRecord('sys_user');
    usr.addQuery('sys_id', row.owners);
    usr.query();
    usr.next(); {
        mrvsexp.push(row.name + ',' + row.address + ',' + row.phone_number + '\r\n');
    }
}

var attachment = new Attachment();
var attachmentRec = attachment.write('sc_task', current.sys_id, 'DEV export.csv', 'text/csv', mrvsexp.join(''));

var att = new GlideRecord('sys_attachment');
att.addQuery('file_name', 'DEV export.csv');
att.addQuery('table_sys_id', current.sys_id);
att.orderByDesc('sys_created_on');
att.query();
if (att.next()) {
    var snEnv = gs.getProperty('instance_name');
    var attsysid = att.sys_id.toString();
    var URL = "https://" + snEnv + ".service-now.com/sys_attachment.do?sysparm_referring_url=tear_off&view=true&sys_id=" + attsysid;
    current.update();
    action.setRedirectURL(URL);
} else {
    current.update();
    action.setRedirectURL(current);
}

 

 

 

Feasood_1-1722460588435.png

 

I have discovered the issue I was having.  I had to make sure the MRVS was showing on the Catalog Task in my Flow, as shown below.  Once I had added that to the Task info, running the UI Action now populates the spreadsheet fields.

 

Feasood_0-1722482037227.pngFeasood_1-1722482055092.png

 

Thank you for your help TJ!