Scheduled scripts for reports with notifications - Better method?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 03:58 PM
I have a requirement to generate an email daily in the afternoon to individuals who are assigned incidents that are in an opened state that have not been updated today.
I have been able to accomplish this through a scheduled job that calls a script include that fires an event which initiates a custom email notification and associated email notification script. I will provide some of the information down below for anyone curious but my question is, is there a better, more optimal way to accomplish this?
I cannot simply have a report emailed to the individuals, the incidents must be printed out in a list in the email to them. I do not see that I can easily perform this through Flow Designer, only the below conventional method. There are many steps involved with potential points of failure (although I haven't seen this process fail yet).
Script Include titled IncidentReminderUtil (or global.IncidentReminderUtil to be more specific)
var IncidentReminderUtil = Class.create();
IncidentReminderUtil.prototype = {
initialize: function() {
},
openedIncidents: function() {
// Create an array to store Assigned To users
var assignedIncidents = [];
// Query Incidents in an open state assigned to an individual
var gr = new GlideRecord('incident');
gr.addEncodedQuery('stateIN1,2,9,4^sys_updated_onNOTONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^assigned_toISNOTEMPTY');
gr.query();
// Iterate through the incidents and populate the object
while (gr.next()) {
// Get Assigned To's sys_id and name
var assignedToSysID, assignedToName, incidentSysID, incidentNumber, incidentShortDescription;
assignedToSysID = gr.assigned_to.getValue();
assignedToName = gr.assigned_to.getDisplayValue();
incidentSysID = gr.sys_id.getValue();
incidentNumber = gr.number.getValue();
incidentShortDescription = gr.short_description.getValue();
// Is the Assigned To individual already in the assigned incidents list
var assignedFound = false;
for(var i = 0; i < assignedIncidents.length; i++) {
if(assignedIncidents[i].assignedTo.sys_id == assignedToSysID) {
assignedFound = true;
break;
}
}
// If the assigned to individual is not found in the list, add them and the associated incident information
if(!assignedFound) {
assignedIncidents.push({
assignedTo: {
sys_id: assignedToSysID,
name: assignedToName,
},
incidents: [{
sys_id: incidentSysID,
number: incidentNumber,
short_description: incidentShortDescription
}],
incCount: 1
});
} else {
// If the assigned to individual is found, update the incidents with the next incident
assignedIncidents[i].incidents.push({
sys_id: incidentSysID,
number: incidentNumber,
short_description: incidentShortDescription
});
// Update amount of assigned incidents
assignedIncidents[i].incCount++;
}
}
// Return the list of incidents
return assignedIncidents;
},
type: 'IncidentReminderUtil'
};
Scheduled Script Execution:
// Get list of incidents not updated today and send notifications to the assigned to individuals
var util = new global.IncidentReminderUtil();
var arr = util.openedIncidents();
for(var i = 0; i < arr.length; i++) {
gs.eventQueue('notify.opened.incidents.assigned', null, arr[i].assignedTo.sys_id, JSON.stringify(arr[i].incidents));
}
Part of the Email Notification Script to parse data: (purposefully left out some information specific to our organization)
// Get user details
var userGR = new GlideRecord('sys_user');
var found = userGR.get(event.parm1);
var name = "User";
if (found) {
// Get actual name of assigned to individual
name = userGR.getDisplayValue();
}
// List of Incidents for assigned to individual
var incList = JSON.parse(event.parm2);
for(var i = 0; i < incList.length; i++) {
// Can use incList[i].number/short_description to print information out
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 06:43 PM - edited 12-19-2023 06:54 PM
Hi @Michael H1 ,
Based on your requirement and details you shared, seems like you already have better way to manage such modular custom notification setup.
For incidents printing on email, you can add template.print() method in email script and use this email script in notification using ${mailscript.<script_name>} , the template.print() method will print all valid INC in email body.
// List of Incidents for assigned to individual
var incList = JSON.parse(event.parm2);
for(var i = 0; i < incList.length; i++) {
// Can use incList[i].number/short_description to print information out
template.print(incList[i].number+'\n');
}
-Thanks,
AshishKMishra
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 06:46 PM
Hi @Michael H1
This looks good. The other way you can do this is, you can handle all this directly in the Email Script.
Just trigger the event with user parameter (may be param1) and do all the business logic in Email Script and print the incidents data.
Thi way you can use this from Flow designer too.
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh