0

var Headers = ["Number","Requested for","Description","Assignment Group", "Assigned To"];
var fileName = 'IncidentRecord.csv';
var csvData = ";
for (var i = 0; i < Headers.length; i++) { //Build the Headers
csvData = csvData + '"' + Headers[i] + '"' + ',';
}
csvData = csvData+"\r\n";

var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while(gr.next()) {
csvData = csvData + '"' + gr.number + '",' + '"' + gr.caller_id.getDisplayValue() + '",' + '"' + gr.description+'",' + '"' + gr.assignment_group.getDisplayValue() + '",' + '"' + gr.assigned_to.getDisplayValue() + '"';
csvData = csvData+"\r\n";
}

//attach the file to a record.
var grRec = new GlideRecord("incident");
grRec.addQuery("sys_id","record_sys_id");
grRec.query();
if(grRec.next()){
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName, 'application/csv',csvData);
}

How to Generate CSV file through script ServiceNow
Working Code Asked question July 4, 2023