Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hello Ansar,

 

Thanks for the inputs. But the file is coming as csv but I am looking for XLS.

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);

Hello Ansar,

 

It works like a charm!!!! Thanks a lot for all the support 🙂

Hi Ansar,

 

I have a similar requirement to the above and additionally I want to import multiple line of data from variable. I have written below code but it is only importing 2 rows from variable sets even if records are 5. 

 

Following is the script for same.

Note: source_issuing_plant is variable set name and it contains 3variables: comment.plant and destination plant. and this variable set has multiple rows.

 

 

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

function generateData() {
var headers = ["Requested ITEM", "Opened By", "Comment", "Source Plant", "Destination Plant", "Request"];
var fileName = 'Plant SPK' + '.csv'; //current.number + '.xls';
var xlsData = '';

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

var comment = current.variables.source_issuing_plant.Comment;
var plant = current.variables.source_issuing_plant.source_issuing_plant;
var Destination_Plant = current.variables.source_issuing_plant.Destination_Plant;

var contents =[comment,plant,Destination_Plant ];
for (var j = 0; j < contents.length-1; j++)
{

// add data to xlsData..
xlsData += current.number + '\t'+ '\t'+ ','+ current.opened_by.getDisplayValue() + '\t'+ '\t'+ ','+
comment[j] + '\t'+ '\t'+ ',' +
plant[j]+ '\t'+ '\t'+ ',' +
Destination_Plant[j]+ '\t'+ '\t'+ ',' +

current.request.getDisplayValue() +'\n';
}

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/csv', xlsData);
}
}
})(current, previous);