Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ServiceNow Incident CSV Export Email Script

This ServiceNow email script automatically generates and attaches a CSV file containing incident data to email notifications. The script extracts active incidents from your ServiceNow instance, formats them into a structured CSV file, and attaches the file to outbound email notifications, providing recipients with a comprehensive incident report in a portable format.

What This Script Does:
The email script performs the following operations:
Data Extraction: Queries all active incidents from the ServiceNow incident table
CSV Generation: Formats incident data into a structured CSV file with predefined headers
File Attachment: Automatically attaches the generated CSV file to email notifications
Dynamic Content: Creates fresh data exports each time the notification is triggered
Portable Format: Provides incident data in a universally readable CSV format
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

var Headers = ["Number", "Caller", "Short Desc", "Assignment Group", "Assigned To"];
var currentDate = new GlideDateTime();
var fileName = 'Incidents' + '.csv';
var csvData = ''; //The variable csvData will contain a string which is used to build the CSV file contents
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.short_description + '",' + '"' + gr.assignment_group.getDisplayValue() + '",' + '"' + gr.assigned_to.getDisplayValue() + '"';
csvData = csvData + "\r\n";
}

var grAttachment = new GlideSysAttachment();
grAttachment.write(event, fileName, 'application/csv', csvData);

})(current, template, email, email_action, event);
Loading