Skip to content

Instantly share code, notes, and snippets.

@cmcdevitt
Created August 17, 2023 19:09
Show Gist options
  • Save cmcdevitt/6c0cd6f99563d146e657b2aa57e31396 to your computer and use it in GitHub Desktop.
Save cmcdevitt/6c0cd6f99563d146e657b2aa57e31396 to your computer and use it in GitHub Desktop.
create and attach csv file from a script
var fileName = 'Incidents.csv';
var csvData = ''; //The variable csvData will contain a string which is used to build the CSV file contents
var Headers = ["Number","Caller","Short Desc","Assignment Group", "Assigned To"];
//Build Header / End First Row
for (var i = 0; i < Headers.length; i++) { //Build the Headers
csvData = csvData + '"' + Headers[i] + '"' + ',';
}
csvData = csvData+"\r\n";//End first row
var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.setLimit(10);
gr.query();
while(gr.next()) {
//Create row data
csvData = csvData + '"' + gr.number + '",' + '"' + gr.caller_id.getDisplayValue() + '",' + '"' + gr.short_description+'",' + '"' + gr.assignment_group.getDisplayValue() + '",' + '"' + gr.assigned_to.getDisplayValue() + '"';
csvData = csvData+"\r\n";//End the Row
}
//attach the file to a record.
var grRec = new GlideRecord("incident");
grRec.addQuery("sys_id","f12ca184735123002728660c4cf6a7ef");//INC0007001
grRec.query();
if(grRec.next()){
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName, 'application/csv',csvData);
}// sn-scriptsync - Received from background script tab via SN Utils. (delete file after usage.)
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment