- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 06:39 AM
Hello,
I must create a button on the Request form. When you click on this button it should generate an Excel file (.csv format) with some informations from Request (price and request number).
So to do this I created an UI action. I checked it in the background script and I saw that it finds the necessary informations from the array (price, request number), but it doesn't generate an Excel file.
var recordId = "c4c75ae58741c9502e93c8490cbb3529";//Testing on one record but should be dynamic.
var tableau = [];
var theTable = "sc_request";
var reqInfo = new GlideRecord(theTable);
reqInfo.addEncodedQuery('active=true');
reqInfo.query();
while (reqInfo.next()) {
var reqObj = {};
reqObj.price = reqInfo.price.toString(),
reqObj.number = reqInfo.number.toString();
tableau.push(reqObj);
}
//gs.print(JSON.stringify(tableau));
writeAttachmentFile(tableau);
function writeAttachmentFile(data) {
var attachment = new Attachment();
var attachmentRec = attachment.write(theTable, recordId, "export.csv", "text/csv", data);
}
Can somebody help me with it? I am new to coding.
Thank you,
Elena
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 07:21 AM
Hi
To use Attachment API you need to provide the data in String format and you were sending an object. Since you want a .csv file the below-provided code might help you:
var recordId = "c4c75ae58741c9502e93c8490cbb3529";
var recGr = new GlideRecord('<Table_Name_0f_RecorId_goes_here>');
recGr.get(recordId);
//Prepare CSV data
var csvHeader = ['Request No.', 'Price'],
csvData = '';
fileName = 'Request Information.csv',
theTable = "sc_request";
//Prepare CSV Header
for(var i = 0; i < csvHeader.length; i++) {
csvData += '"' + csvHeader[i] + '"' + ',';
}
csvData += '\r\n';
//Prepare records
var reqInfo = new GlideRecord(theTable);
reqInfo.addEncodedQuery('active=true');
reqInfo.query();
while (reqInfo.next()) {
csvData += '"' + reqInfo.getValue('number') + '"' + ',' + '"' + reqInfo.getValue('price') + '"' + '\r\n';
}
//gs.print(JSON.stringify(tableau));
writeAttachmentFile(recGr, fileName, csvData);
function writeAttachmentFile(recObj, fileName, data) {
var grAttachment = new GlideSysAttachment();
grAttachment.write(recObj, fileName, 'application/csv',data);
}
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 06:45 AM
Can you change
var attachmentRec = attachment.write(theTable, recordId, "export.csv", "text/csv", data);
to
var attachmentRec = attachment.write(theTable, recordId+"_export.csv", "text/csv", data);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 07:21 AM
Hi
To use Attachment API you need to provide the data in String format and you were sending an object. Since you want a .csv file the below-provided code might help you:
var recordId = "c4c75ae58741c9502e93c8490cbb3529";
var recGr = new GlideRecord('<Table_Name_0f_RecorId_goes_here>');
recGr.get(recordId);
//Prepare CSV data
var csvHeader = ['Request No.', 'Price'],
csvData = '';
fileName = 'Request Information.csv',
theTable = "sc_request";
//Prepare CSV Header
for(var i = 0; i < csvHeader.length; i++) {
csvData += '"' + csvHeader[i] + '"' + ',';
}
csvData += '\r\n';
//Prepare records
var reqInfo = new GlideRecord(theTable);
reqInfo.addEncodedQuery('active=true');
reqInfo.query();
while (reqInfo.next()) {
csvData += '"' + reqInfo.getValue('number') + '"' + ',' + '"' + reqInfo.getValue('price') + '"' + '\r\n';
}
//gs.print(JSON.stringify(tableau));
writeAttachmentFile(recGr, fileName, csvData);
function writeAttachmentFile(recObj, fileName, data) {
var grAttachment = new GlideSysAttachment();
grAttachment.write(recObj, fileName, 'application/csv',data);
}
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 11:34 PM
Hi
Was I able to answer your question?
If yes and if my answer helped you out then please mark my answer as correct/helpful, so that other community members facing similar issues might get help.
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2022 01:09 AM
Hello Kartik,
Your script works perfectly.
Thank you very much!!!
Best wishes,
Elena