- 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 05:39 AM
Could you please provide more information? What do you mean by attach to RITM and from where do you want to export the fields?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 05:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 06:06 AM - edited 02-09-2023 06:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 06:28 AM
Hello Ansar,
Excel is getting generated but field values are not coming in different columns. PFB the snapshot: