Trigger email based on the schedule and conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2023 10:19 AM - edited 10-31-2023 05:56 AM
Hi All,
I need help with a ServiceNow case, I have the following email scheduling requirements:
1) Schedule emails to be sent every Monday at 10 AM.
2) These emails should contain 'URLs to a list of incidents and RITMs records' , probably hard coded because each group has different query filter. (No Excel or PDF attachments)
3) The email should only be triggered if there are records (incidents or RITMs) greater than 0; otherwise, it should be omitted.
4) Send separate emails for 13 different groups, each with their respective URLs.
5) The recipient list based will be on the group.
Is this doable? Can someone help me with approach and code if possible, Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2023 10:29 AM
Yes. It is doable using a scheduled job or flow designer. We have done something similar in our instance.
First you need to query the task table with incident and RITM as class in filter and get all the unique groups you need to send emails to.
So it will not pull any group which has 0 assignment.
Once you have all the groups, trigger events for each group. Associate the event to a notification.
This way it will trigger notification for each group.
Now to get the URL, create mailscript to query the task table again only for that group and use the mailscript in notification.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2023 10:33 AM
Hello @Query1 ,
Yes, it is doable. You can use scheduled job/ flow to send mail every Monday 10 AM and and trigger the notification using event.
Please Mark my answers Helpful & Accepted if I have answered your questions.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2023 10:43 AM
Hi @Query1 ,
// Scheduled Job Script (Scheduled to run every Monday at 10 AM)
var gr = new GlideRecord('incident');
var emailBody = '';
// Define your 13 groups here with their respective conditions
var groups = [
{ name: 'Group1', condition: 'YOUR_GROUP_CONDITION_1' },
{ name: 'Group2', condition: 'YOUR_GROUP_CONDITION_2' },
// ... and so on for other groups
];
groups.forEach(function(group) {
// Query incidents based on group condition
gr.addQuery('group', group.condition);
gr.query();
// Construct URLs for incidents
var incidentUrls = [];
while (gr.next()) {
incidentUrls.push(gs.getProperty('glide.servlet.uri') + 'incident.do?sys_id=' + gr.sys_id);
}
// If there are incidents, add them to the email body
if (incidentUrls.length > 0) {
emailBody += 'Incidents for ' + group.name + ':\n';
emailBody += incidentUrls.join('\n') + '\n\n';
}
});
// Send email if there is data to include
if (emailBody) {
gs.eventQueue('event_name',gr,emailBody,'receipient email');
}
The above script is untested n developed based upon ur requirement explained. Kindly modify n use as per requirement.
Thanks,
Danish