How to add a weekly digest to remind users they have tasks assigned to them and have open approvals.
Be careful with these scripts and always test in a development instance first. You want to make sure it doesn't send duplicate emails or have an email loop.
Event Registration
Name: servicenow.digest
Fired by: ServiceNow Digest Scheduled Job
Description: ServiceNow Weekly Digest
Notification
Name: ServiceNow Weekly Digest
Table: User [sys_user]
Description: triggered by: ServiceNow Digest Scheduled Job
sent to: Users
purpose: ServiceNow Digest
When to send:
Send when: Event is fired
Event Name: servicenow.digest
Who will Receive:
Send to event creator: true
Event Parm 2 contains recipient: true
What it will contain:
Content Type: HTML and plain text
Message HTML
<div>$,</div>
<div> </div>
<div>As a frequent user of ServiceNow, you receive this weekly digest for your ServiceNow activity.</div>
<div> </div>
<div>
<div><hr /></div>
</div>
<div> </div>
<div><span style="font-size: 16pt;"><strong>Your Latest Activity</strong></span></div>
<div> </div>
<div>${mail_script:ServiceNow Weekly Digest_script_1}</div>
Email Script
Name: ServiceNow Weekly Digest_script_1
Script:
myActivity();
function myActivity() {
//Send list of closed Tasks
var grClosedTasks = new GlideRecord('task');
grClosedTasks.addQuery('closed_by',current.sys_id);
grClosedTasks.addEncodedQuery('active=false^closed_atONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()^sys_class_name!=sc_request^sys_class_name!=sysapproval_group');
grClosedTasks.query();
template.print('<a href="https://' + gs.getProperty('instance_name') + '.service-now.com/task_list.do?sysparm_query=closed_by%3Djavascript%3AgetMyAssignments()%5Eactive%3Dfalse%5Eclosed_atONThis%20week%40javascript%3Ags.beginningOfThisWeek()%40javascript%3Ags.endOfThisWeek()%5Esys_class_name!%3Dsc_request%5Esys_class_name!%3Dsysapproval_group">'+ "<strong>Your Closed Tasks This Week</strong></a>"+"\n"+"\n");
template.print("You have closed <strong>"+grClosedTasks.getRowCount()+"</strong> tasks this past week."+"\n");
template.print("<ul>");
while (grClosedTasks.next()) {
template.print("<li>");
template.print('Task: <a href="https://' + gs.getProperty('instance_name') + '.service-now.com/task.do?sys_id='+grClosedTasks.sys_id+'">'+ grClosedTasks.number +"</a> - "+grClosedTasks.short_description);
template.print("</li>");
}
template.print("</ul>");
template.print("\n"+"\n");
//Send list of open Tasks
var grOpenTasks = new GlideRecord('task');
grOpenTasks.addQuery('assigned_to',current.sys_id);
grOpenTasks.addEncodedQuery('active=true');
grOpenTasks.query();
template.print('<a href="https://' + gs.getProperty('instance_name') + '.service-now.com/task_list.do?sysparm_query=assigned_to%3Djavascript%3AgetMyAssignments()%5Eactive%3Dfalse">'+ "<strong>Your Open Tasks</strong></a>"+"\n"+"\n");
template.print("You have <strong>"+grOpenTasks.getRowCount()+"</strong> tasks assigned to you."+"\n");
template.print("<ul>");
while (grOpenTasks.next()) {
template.print("<li>");
template.print('Task: <a href="https://' + gs.getProperty('instance_name') + '.service-now.com/task.do?sys_id='+grOpenTasks.sys_id+'">'+ grOpenTasks.number +"</a> - "+grOpenTasks.short_description);
template.print("</li>");
}
template.print("</ul>");
template.print("\n"+"\n");
//Send list of requested Approvals
var grApprover = new GlideRecord('sysapproval_approver');
grApprover.addQuery('state','requested');
grApprover.addQuery('approver',current.sys_id);
grApprover.query();
template.print('<a href="https://' + gs.getProperty('instance_name') + '.service-now.com/sysapproval_approver_list.do?sysparm_query=approver%3Djavascript%3AgetMyApprovals()%5Estate%3Drequested">'+ "<strong>Your Approvals</strong></a>"+"\n"+"\n");
template.print("You have <strong>"+grApprover.getRowCount()+"</strong> requested approvals waiting in ServiceNow."+"\n");
template.print("<ul>");
while (grApprover.next()) {
template.print("<li>");
template.print('Approval for: <a href="https://' + gs.getProperty('instance_name') + '.service-now.com/sysapproval_approver.do?sys_id='+grApprover.sys_id+'">'+ grApprover.sysapproval.number +"</a> - "+grApprover.sysapproval.short_description);
template.print("</li>");
}
template.print("</ul>");
template.print("\n"+"\n");
//Send list of Changes Past Due
var grChange = new GlideRecord('change_request');
grChange.addQuery('assigned_to',current.sys_id);
grChange.addEncodedQuery('active=true^end_date<javascript:gs.daysAgoStart(0)');
grChange.query();
template.print('<a href="https://' + gs.getProperty('instance_name') + '.service-now.com/change_request_list.do?sysparm_query=assigned_to%3Djavascript%3AgetMyAssignments()%5EORrequested_by%3Djavascript%3Ags.getUserID()%5Eactive%3Dtrue%5Eend_date%3Cjavascript%3Ags.daysAgoStart(0)">'+ "<strong>Your Changes Past Due</strong></a>"+"\n"+"\n");
template.print("You have <strong>"+grChange.getRowCount()+"</strong> changes open past due in ServiceNow."+"\n");
template.print("<ul>");
while (grChange.next()) {
template.print("<li>");
template.print('Change Request: <a href="https://' + gs.getProperty('instance_name') + '.service-now.com/change_request.do?sys_id='+grChange.sys_id+'">'+ grChange.number +"</a> - "+grChange.short_description);
template.print("</li>");
}
template.print("</ul>");
template.print("\n"+"\n");
//Send list of Flagged Knowledge Articles
var myUser = gs.getUser().getUserByID(current.sys_id.toString());
var grKB = new GlideRecord('kb_knowledge');
grKB.addQuery('flagged','true');
grKB.addQuery('author',current.sys_id);
grKB.query();
template.print('<a href="https://' + gs.getProperty('instance_name') + '.service-now.com/kb_knowledge_list.do?sysparm_query=flagged%3Dtrue%5Eu_kb_ownership_group%3Djavascript%3AgetMyGroups()">'+ "<strong>Your Flagged Knowledge Articles</strong></a>"+"\n"+"\n");
template.print("You have <strong>"+grKB.getRowCount()+"</strong> articles flagged in ServiceNow."+"\n");
template.print("<ul>");
while (grKB.next()) {
template.print("<li>");
template.print('Knowledge Article: <a href="https://' + gs.getProperty('instance_name') + '.service-now.com/kb_knowledge.do?sys_id='+grKB.sys_id+'">'+ grKB.number +"</a> - "+grKB.short_description);
template.print("</li>");
}
template.print("</ul>");
template.print("\n"+"\n");
template.print("<hr/>");
template.print("<br />");
template.print('<a href="' + gs.getProperty('glide.servlet.uri')+ 'unsubscribe.do?sysparm_notification=' + email_action.sys_id+ '">Unsubscribe from ServiceNow Weekly Digest email</a><br />\n');
}
Scheduled Job
Name: ServiceNow Weekly Digest
Run: Weekly
Day: Friday
Hours: 11 00 00
Script:
sendWeeklyDigest();
function sendWeeklyDigest() {
//Setup Array Functions
var au = new ArrayUtil();
var users = [];
//1 Find Users with Open Tasks
var grOpenTask = new GlideRecord('task');grOpenTask.addEncodedQuery('active=true^assigned_toISNOTEMPTY^assigned_to.active=true');
grOpenTask.query();
while (grOpenTask.next())
//2 Find Users with Requested Approvals
var grApprover = new GlideRecord('sysapproval_approver');
grApprover.addEncodedQuery('state=requested');
grApprover.query();
while (grApprover.next())
//3 Send Users Email
//gs.log("User Count: "+users.length);
users = au.unique(users);
//gs.log("Unique User Count: "+users.length);
//gs.log("Unique Users: "+users);
if (users.length > 0) {
for (var i=0; i<users.length; i++) {
gs.eventQueue('servicenow.digest', 'sys_user',users[i], gs.now(), users[i]);
}
}
}