diff --git a/Server-Side Components/Scheduled Jobs/Scheduled Job to Email Incident Count Report by Category/Readme.md b/Server-Side Components/Scheduled Jobs/Scheduled Job to Email Incident Count Report by Category/Readme.md new file mode 100644 index 0000000000..64f34b42eb --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Scheduled Job to Email Incident Count Report by Category/Readme.md @@ -0,0 +1,34 @@ +Scheduled Job: Sends an email with monthly report based on incident category count + +Uses a custom event (custom.monthly.incident.report) with two parameters: +parm1 → The formatted incident count report body +parm2 → The month name + +Working: +Runs automatically on the 1st of each month. +Fetches all incidents created in the previous month. +Groups them by category and counts totals. +Sends a summarized email report to the admin. + +Event Registration +Name: custom.monthly.incident.report +Queue: default + +Notification Configuration +Name: Monthly Incident Report by Category +When to send: Event is fired +Event name: custom.monthly.incident.report +Recipients: admin@example.com (or “Admin” group) + +Subject +Monthly Incident Count Report - ${event.parm2} + +Body + +Hello Admin, + +Here is the count of incidents created during ${event.parm2}, categorized by type: +${event.parm1} + +Regards, +ServiceNow Automated Reports diff --git a/Server-Side Components/Scheduled Jobs/Scheduled Job to Email Incident Count Report by Category/Send_monthly_incident_category_count_report.js b/Server-Side Components/Scheduled Jobs/Scheduled Job to Email Incident Count Report by Category/Send_monthly_incident_category_count_report.js new file mode 100644 index 0000000000..66d6c3f915 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Scheduled Job to Email Incident Count Report by Category/Send_monthly_incident_category_count_report.js @@ -0,0 +1,47 @@ +(function() { + // Step 1: Define the date range for the previous month + var startOfMonth = new GlideDateTime(); + startOfMonth.setValue(gs.beginningOfLastMonth()); + + var endOfMonth = new GlideDateTime(); + endOfMonth.setValue(gs.endOfLastMonth()); + + // Step 2: Query all incidents created in that month + var gr = new GlideRecord('incident'); + gr.addQuery('opened_at', '>=', startOfMonth); + gr.addQuery('opened_at', '<=', endOfMonth); + gr.query(); + + // Step 3: Build a map of category counts + var categoryCount = {}; + while (gr.next()) { + var category = gr.category ? gr.category.toString() : 'Uncategorized'; + categoryCount[category] = (categoryCount[category] || 0) + 1; + } + + // Step 4: Build report body + var reportBody = ''; + var total = 0; + + for (var categoryName in categoryCount) { + total += categoryCount[categoryName]; + reportBody += categoryName + ': ' + categoryCount[categoryName] + '\n'; + } + + if (total === 0) { + reportBody = 'No incidents were created in the last month.'; + } else { + reportBody = 'Total Incidents: ' + total + '\n\n' + reportBody; + } + + // Step 5: Add month name for better readability + var monthName = gs.getMonthName(gs.monthsAgo(1)); + + // Step 6: Trigger custom event to send email + // parm1 = report body + // parm2 = month name + gs.eventQueue('custom.monthly.incident.report', null, reportBody, monthName); + + gs.info('Monthly Incident Report event triggered for ' + monthName); + +})();