Skip to content

Commit e140432

Browse files
Scheduled job ServiceNow script
Monthly scheduled job to send email report based on incident count category
1 parent e7466f7 commit e140432

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
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)