From 6ca690ca1a70507b363f4b05273eaa148c8f2e02 Mon Sep 17 00:00:00 2001 From: prudvi-sai-posetty Date: Tue, 28 Oct 2025 14:53:08 +0530 Subject: [PATCH 1/2] emailAttachment.js --- .../emailAttachment.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Server-Side Components/Server Side/Generate Attachment and add it to the email/emailAttachment.js diff --git a/Server-Side Components/Server Side/Generate Attachment and add it to the email/emailAttachment.js b/Server-Side Components/Server Side/Generate Attachment and add it to the email/emailAttachment.js new file mode 100644 index 0000000000..27bd543c70 --- /dev/null +++ b/Server-Side Components/Server Side/Generate Attachment and add it to the email/emailAttachment.js @@ -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); From 05b4f28034f44acfefd27ea1262df79a1f4c6431 Mon Sep 17 00:00:00 2001 From: prudvi-sai-posetty Date: Tue, 28 Oct 2025 14:57:03 +0530 Subject: [PATCH 2/2] README.md --- .../README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Server-Side Components/Server Side/Generate Attachment and add it to the email/README.md diff --git a/Server-Side Components/Server Side/Generate Attachment and add it to the email/README.md b/Server-Side Components/Server Side/Generate Attachment and add it to the email/README.md new file mode 100644 index 0000000000..f836fc1608 --- /dev/null +++ b/Server-Side Components/Server Side/Generate Attachment and add it to the email/README.md @@ -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