- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 05:33 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 09:13 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 08:35 AM
Hello Ansar,
Thanks for the inputs. But the file is coming as csv but I am looking for XLS.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 09:13 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 09:22 AM
Hello Ansar,
It works like a charm!!!! Thanks a lot for all the support 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2023 06:03 AM
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);