to send a email notification with count of open sc tasks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 05:55 AM
I want to send a email notification every 6 hours. The email body will contain the count of all open tasks , the tasks number and short descriptions of those open tasks. Please help urgent.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 06:02 AM
better to create a flow for this.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 06:09 AM
you can use scheduled job which runs periodically every 6 hours.
You can configure event, email etc on your INC table
Then use this approach on how to send 1 email to 1 user containing list of all tasks/incidents
I have given example for alm_asset table you can enhance for your table
1) Create event on alm_asset table
2) Create Notification on alm_asset table and link the above event created
3) Use the script shared below in scheduled job to trigger email per user
4) Create email Script and in that you can print the list of Assets Assigned to that particular user for which email triggered.
1) Event
Name: send.email.assignedToUser.asset
Table: alm_asset
2) Email Script:
Name: assets_assigned
(function runMailScript(current, template, email, email_action, event) {
var user = event.parm1;
var arr = [];
var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', user);
gr.query();
while(gr.next()){
arr.push(gr.display_name.toString());
}
template.print('Assets assigned to you are: ' + arr.toString());
})(current, template, email, email_action, event);
3) Job Script:
sendEmail();
function sendEmail(){
var gr = new GlideAggregate("alm_asset");
gr.addAggregate("COUNT");
gr.addEncodedQuery('assigned_toISNOTEMPTY');
gr.groupBy("assigned_to");
gr.query();
while(gr.next()) {
var user = gr.assigned_to;
var assetRec = new GlideRecord('alm_asset');
assetRec.get('assigned_to', user);
gs.eventQueue("send.email.assignedToUser.asset", assetRec, user, "");
}
}
4) Notification:
Body:
${mail_script:assets_assigned}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 06:17 AM
The question really is: why? What is the upside to doing this? People will check the email the first two times and then ignore it. And how can sending an email be urgent? If someone really wants this, they can also log in to the system every 6 hours.
And for the solution: why not use a scheduled report? Just create a report on the table you need, add the fields you need and schedule it to run every 6 hours. No need for scripting at all.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2025 06:36 AM
simply use this
(function() {
// Query to get all open tasks
var taskGR = new GlideRecord('incident');
taskGR.addActiveQuery();
taskGR.addEncodedQuery('assigned_to!=NULL');
taskGR.setLimit(10);
taskGR.query();
while (taskGR.next()) {
var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.addEncodedQuery('assigned_to=' + taskGR.assigned_to);
gr.query();
var taskCount = gr.getRowCount();
// Create the email body
var emailBody = "There are " + taskCount + " open tasks for you:\n\n";
emailBody += "Task Number: " + taskGR.getValue('number') + "\n";
emailBody += "Short Description: " + taskGR.getValue('short_description') + "\n\n";
// Send the email
var email = new GlideEmailOutbound();
email.setSubject("Open Tasks Notification");
email.setBody(emailBody);
email.setRecipients(taskGR.assigned_to.email.toString()); // Replace with your recipient email
email.save();
}
})();
I ran this for incident and here is my email log
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader