Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);

})();
Loading