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 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);