diff --git a/Server-Side Components/Scheduled Jobs/Export Filtered Records to CSV Automatically/README.md b/Server-Side Components/Scheduled Jobs/Export Filtered Records to CSV Automatically/README.md new file mode 100644 index 0000000000..1792f0e745 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Export Filtered Records to CSV Automatically/README.md @@ -0,0 +1,2 @@ +This code_snippet.js script export incidents of the last 7 days and email the CSV. +This script will be created as Scheduled Script. diff --git a/Server-Side Components/Scheduled Jobs/Export Filtered Records to CSV Automatically/code_snippet.js b/Server-Side Components/Scheduled Jobs/Export Filtered Records to CSV Automatically/code_snippet.js new file mode 100644 index 0000000000..4cb3196520 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Export Filtered Records to CSV Automatically/code_snippet.js @@ -0,0 +1,12 @@ +// Below script export incidents of the last 7 days and email the CSV using Scheduled Script +var fileName = 'incident_export_' + gs.nowDateTime() + '.csv'; +var csv = 'Number,Short Description,Priority\n'; +var gr = new GlideRecord('incident'); +gr.addQuery('opened_at', '>=', gs.daysAgoStart(7)); +gr.query(); +while (gr.next()) { + csv += gr.number + ',' + gr.short_description + ',' + gr.priority + '\n'; +} +var attachment = new GlideSysAttachment(); +attachment.write('incident', '', fileName, csv); +gs.eventQueue('csv.report.ready', null, fileName, gs.getUserID());