Export specific fields as excel and attach on RITM

basavaraja
Tera Contributor

Hello Geeks,

 

We have a requirement where in we need to export few specific fields to excel and attach on RITM. Please let us know if anyone has implemented this requirement?

1 ACCEPTED SOLUTION

Hello,
Test this

 

(function executeRule(current, previous /*null when async*/ ) {
generateData();
function generateData() {
var headers = ["Requested ITEM", "Opened By", "Request"];
var fileName = current.number + '.xls';
var xlsData = '';

// add headers to xlsData
for (var i = 0; i < headers.length; i++) {
xlsData += headers[i] + '\t';
}
xlsData += '\n';

// add data to xlsData
xlsData += current.number + '\t' + current.opened_by.getDisplayValue() + '\t' + current.request.getDisplayValue() + '\n';

// attach the file to a record.
var grRec = new GlideRecord("sc_req_item");
grRec.addQuery("sys_id", current.sys_id);
grRec.query();
if (grRec.next()) {
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName, 'application/vnd.ms-excel', xlsData);
}
}
})(current, previous);

View solution in original post

13 REPLIES 13

Md Saquib Ansar
Giga Guru

Could you please provide more information? What do you mean by attach to RITM and from where do you want to export the fields?

Hello Ansar,

Thanks for the response. Once the RITM is closed complete then few fields on RITM should be exported as Excel and attach on the RITM automatically.

Hi ,
You can try below code to achieve this (Note Tested though)

 

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here

generateData();

}

function generateData() {
var Headers = ["Requested ITEM","Field 1", "Field 2"];


var fileName = 'Your File Name.xls';
var xlsData = '';
for (var i = 0; i < Headers.length; i++) {
xlsData = xlsData + '"' + Headers[i] + '"' + ',';
}
xlsData = xlsData + "\r\n";

 

xlsData = xlsData + '"' + current.number + '",' + '"' + current.field1 + '",' + '"' + current.field2 + '"';


xlsData = xlsData + "\r\n";


//attach the file to a record.
var grRec = new GlideRecord("sc_req_item");
grRec.addQuery("sys_id", current.sys_id);
grRec.query();
if (grRec.next()) {
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName, 'application/xls', xlsData);
}


}

})(current, previous);

Regards
Saquib

Hello Ansar,

Excel is getting generated but field values are not coming in different columns. PFB the snapshot:

 

basavaraja_0-1675952870422.png