Skip to content

Commit 7063922

Browse files
Scheduled job to send email report based on incident count category (#2544)
* Create Readme.md Monthly scheduled job to send email report based on incident category count * Scheduled job ServiceNow script Monthly scheduled job to send email report based on incident count category
1 parent 7c55c08 commit 7063922

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Scheduled Job: Sends an email with monthly report based on incident category count
2+
3+
Uses a custom event (custom.monthly.incident.report) with two parameters:
4+
parm1 → The formatted incident count report body
5+
parm2 → The month name
6+
7+
Working:
8+
Runs automatically on the 1st of each month.
9+
Fetches all incidents created in the previous month.
10+
Groups them by category and counts totals.
11+
Sends a summarized email report to the admin.
12+
13+
Event Registration
14+
Name: custom.monthly.incident.report
15+
Queue: default
16+
17+
Notification Configuration
18+
Name: Monthly Incident Report by Category
19+
When to send: Event is fired
20+
Event name: custom.monthly.incident.report
21+
Recipients: admin@example.com (or “Admin” group)
22+
23+
Subject
24+
Monthly Incident Count Report - ${event.parm2}
25+
26+
Body
27+
28+
Hello Admin,
29+
30+
Here is the count of incidents created during ${event.parm2}, categorized by type:
31+
${event.parm1}
32+
33+
Regards,
34+
ServiceNow Automated Reports
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(function() {
2+
// Step 1: Define the date range for the previous month
3+
var startOfMonth = new GlideDateTime();
4+
startOfMonth.setValue(gs.beginningOfLastMonth());
5+
6+
var endOfMonth = new GlideDateTime();
7+
endOfMonth.setValue(gs.endOfLastMonth());
8+
9+
// Step 2: Query all incidents created in that month
10+
var gr = new GlideRecord('incident');
11+
gr.addQuery('opened_at', '>=', startOfMonth);
12+
gr.addQuery('opened_at', '<=', endOfMonth);
13+
gr.query();
14+
15+
// Step 3: Build a map of category counts
16+
var categoryCount = {};
17+
while (gr.next()) {
18+
var category = gr.category ? gr.category.toString() : 'Uncategorized';
19+
categoryCount[category] = (categoryCount[category] || 0) + 1;
20+
}
21+
22+
// Step 4: Build report body
23+
var reportBody = '';
24+
var total = 0;
25+
26+
for (var categoryName in categoryCount) {
27+
total += categoryCount[categoryName];
28+
reportBody += categoryName + ': ' + categoryCount[categoryName] + '\n';
29+
}
30+
31+
if (total === 0) {
32+
reportBody = 'No incidents were created in the last month.';
33+
} else {
34+
reportBody = 'Total Incidents: ' + total + '\n\n' + reportBody;
35+
}
36+
37+
// Step 5: Add month name for better readability
38+
var monthName = gs.getMonthName(gs.monthsAgo(1));
39+
40+
// Step 6: Trigger custom event to send email
41+
// parm1 = report body
42+
// parm2 = month name
43+
gs.eventQueue('custom.monthly.incident.report', null, reportBody, monthName);
44+
45+
gs.info('Monthly Incident Report event triggered for ' + monthName);
46+
47+
})();

0 commit comments

Comments
 (0)