Skip to content

Commit fa476f9

Browse files
Incident CSV Export Email Script (#2570)
* emailAttachment.js * README.md
1 parent a1f42f4 commit fa476f9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ServiceNow Incident CSV Export Email Script
2+
3+
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.
4+
5+
What This Script Does:
6+
The email script performs the following operations:
7+
Data Extraction: Queries all active incidents from the ServiceNow incident table
8+
CSV Generation: Formats incident data into a structured CSV file with predefined headers
9+
File Attachment: Automatically attaches the generated CSV file to email notifications
10+
Dynamic Content: Creates fresh data exports each time the notification is triggered
11+
Portable Format: Provides incident data in a universally readable CSV format
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
2+
/* Optional EmailOutbound */
3+
email, /* Optional GlideRecord */ email_action,
4+
/* Optional GlideRecord */
5+
event) {
6+
7+
var Headers = ["Number", "Caller", "Short Desc", "Assignment Group", "Assigned To"];
8+
var currentDate = new GlideDateTime();
9+
var fileName = 'Incidents' + '.csv';
10+
var csvData = ''; //The variable csvData will contain a string which is used to build the CSV file contents
11+
for (var i = 0; i < Headers.length; i++) { //Build the Headers
12+
csvData = csvData + '"' + Headers[i] + '"' + ',';
13+
}
14+
csvData = csvData + "\r\n";
15+
16+
var gr = new GlideRecord("incident");
17+
gr.addActiveQuery();
18+
gr.query();
19+
while (gr.next()) {
20+
csvData = csvData + '"' + gr.number + '",' + '"' + gr.caller_id.getDisplayValue() + '",' + '"' + gr.short_description + '",' + '"' + gr.assignment_group.getDisplayValue() + '",' + '"' + gr.assigned_to.getDisplayValue() + '"';
21+
csvData = csvData + "\r\n";
22+
}
23+
24+
var grAttachment = new GlideSysAttachment();
25+
grAttachment.write(event, fileName, 'application/csv', csvData);
26+
27+
})(current, template, email, email_action, event);

0 commit comments

Comments
 (0)