Email notification using m2m_kb_task table - group by task (INC##)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 05:25 PM
Hi all
I want to create an email notification that is triggered when an article in attached to an incident (m2m_kb_task), and have the email contain all the articles that were attached to that single task. This is the email notification I have created:
I do not know how I can group all the articles into one email as the m2m_kb_task table adds each article separately. So not to spam the recipient with several emails when multiple article are attached to a single task, what conditions can I set to the email notification so that it is one email per task, regardless of how many articles are attached?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:41 AM
I have followed these steps but somehow the email is not going to the outbox once the incident has been resolved. I would also like the links for the articles to direct to the SP rather than KB.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 07:05 PM
Did you follow all the steps?
Did you debug the components?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 02:00 PM
@Ankur Bawiskar yes I followed all the steps. I don't know what you mean by debug the components, sorry.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 07:04 PM
did you add logs and see where it's failing?
Did you check event was triggered and processed?
Any error in email script by adding logs?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 11:41 AM - edited 02-04-2025 12:12 PM
Sorry I am not a developer, but trying to learn some of the basics. I don't know how to check those.
Here is what I have:
Business Rule | |
Name | TC article attached to incident |
Table | m2m_kb_task |
When to run | Insert |
Condition | current.task.number.toString().startsWith('INC') |
Script | (see below) |
(function executeRule(current, previous /*null when async*/ ) {
// Trigger event with a short delay to allow for grouping
var inc = new GlideRecord('incident');
inc.get(current.task);
var kbList = [];
var gr = new GlideRecord('m2m_kb_task');
gr.addQuery('task', current.task);
gr.query();
while (gr.next()) {
var kbArticle = new GlideRecord('kb_knowledge');
if (kbArticle.get(gr.kb_knowledge)) {
kbList.push('<li><a href="' + kbArticle.getDisplayValue('sys_id') + '">' + kbArticle.getDisplayValue('short_description') + '</a></li>');
}
}
if (kbList.length == 0)
return;
else {
// Generate email body
var emailBody = '<p>The following knowledge articles were attached to Incident ' + inc.getDisplayValue() + ':</p>';
emailBody += '<ul>' + kbList.join('') + '</ul>';
gs.eventQueueScheduled(
'tc.articles_attached_to_incident', // Event Name
current, // m2m Record
inc.caller_id.name.toString(), // Parm1 (Optional)
inc.caller_id.email.toString(), // Parm2 recipient
GlideDateTime.add(30 * 1000) // 30 seconds delay
);
}
})(current, previous);
Event Registration | |
Name | tc.articles_attached_to_incident |
Table | m2m_kb_task |
Queue/Script | (none) |
Fired by | TC article attached to incident |
Script | (see below) |
Email Notification | |
Name | Attached TC article email notification |
Table | m2m_kb_task |
When to send | Event is fired: tc.articles_attached_to_incident |
Who will receive | Event param 2 contains recipient |
Message HTML (contents stripped back) | Hi ${event.parm1},
You recently spoke to IT who helped you resolve your technology issue (${task.number}).
For your reference, here are the article(s) shared with you: ${mail_script:tc_email_show_details} |
Script/conditions | (none) |
Email Script | |
Name | tc_email_show_details |
Script | (see below) |
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var inc = new GlideRecord('incident');
inc.get(current.task);
var kbList = [];
var gr = new GlideRecord('m2m_kb_task');
gr.addQuery('task', current.task);
gr.query();
while (gr.next()) {
var kbArticle = new GlideRecord('kb_knowledge');
if (kbArticle.get(gr.kb_knowledge)) {
kbList.push('<li><a href="' + kbArticle.getDisplayValue('sys_id') + '">' + kbArticle.getDisplayValue('short_description') + '</a></li>');
}
}
if (kbList.length == 0)
return;
else {
// Generate email body
var emailBody = '<p>The following knowledge articles were attached to Incident ' + inc.getDisplayValue() + ':</p>';
emailBody += '<ul>' + kbList.join('') + '</ul>';
template.print(emailBody);
}
})(current, template, email, email_action, event);