Email notification using m2m_kb_task table - group by task (INC##)

Keszia
Giga Guru

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:

Keszia_0-1738200156076.png

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?

Keszia_1-1738200294829.png

 

15 REPLIES 15

MC30
Tera Guru

Hi Keszia,

 

Since m2m_kb_task created separate records for each article-incident association, you can use event-driven notification with a delay to consolidate all articles.

 

Step-1: Create an Event

Step-2: Create an After Insert Business Rule (m2m_kb_task table), to fire the event.

Step-3: Create a Script Action to gather and format articles

Step-4: Create an Email Notification on Incident table on 'Event is Fired'.

 

Please let me know if you need help to configure the script action and the BR.

 

If my response helped, please mark it correct and close the thread so that it benefits future readers.

Also please mark it as helpful

 

Thanks,

Madhuri

Thanks @MC30 - I've used script actions for other areas, but this part is new - could you assist with the structure?

Hi @Keszia ,

Glad to help

 

Step 1: Create an Event
1. Navigate to `System Policy > Events > Event Registry`.
2. Click New and create an event:
- Name: `incident.article.attached`
- Table: `m2m_kb_task`
- Description: Triggered when a knowledge article is attached to an incident.
3. Click Submit.

---

Step 2: Create a Business Rule to Fire the Event
1. Navigate to `System Definition > Business Rules`.
2. Click New and configure the following:

- Name: `Trigger Incident Article Notification`
- Table: `m2m_kb_task`
- When: `After Insert`
- Condition: `task.table == "incident"` (Ensures it only runs for Incidents)
- Script:

 

 

(function executeRule(current, previous /*null when async*/) {
// Trigger event with a short delay to allow for grouping
gs.eventQueueScheduled(
'incident.article.attached', // Event Name
current.task, // Incident Record
current.sys_id, // Parm1 (Optional)
'', // Parm2 (Unused)
GlideDateTime.add(30 * 1000) // 30 seconds delay
);
})(current, previous);

 

Step 3:

Create a Script Action to Gather and Format Articles
1. Navigate to System Policy > Events > Script Actions`.
2. Click New and set:
-Event Name: `incident.article.attached`

 

Script:

 

(function executeRule(taskSysId) {
        var incident = new GlideRecord('incident');
        if (!incident.get(taskSysId)) return;
        var kbList = [];
        var gr = new GlideRecord('m2m_kb_task');
        gr.addQuery('task', taskSysId);
        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;
        // Generate email body
        var emailBody = '<p>The following knowledge articles were attached to Incident ' + incident.getDisplayValue('number') + ':</p>';
        emailBody += '<ul>' + kbList.join('') + '</ul>';
        // Send Notification
        gs.eventQueue('incident.article.notification', incident, emailBody);
    })(event.parm1);

 

 

Step 4: Create an Email Notification
  - Name: `Incident Article Attachment Notification`  
  - Table: `Incident`  
  - When to Send: Select "Event is Fired", then choose `incident.article.notification`.  
  - Who will receive: Assign appropriate recipients (`caller_id`, `assigned_to`, etc.).  
  - Message Body: 
    ```
    ${event.parm1}
    ``` 

 

Please mark it as helpful and mark it correct if my solution helped you.

 

Regards,

Madhuri

Thanks so much @MC30.

For the Business Rule - I can't see anything explicitly labelled "after Insert", but I can see checkboxes for Insert or Update, is ticking Insert sufficient?

----

Here is what I have, but it's not triggering the email:

  • Event "tc.articles_attached_to_incident"

Keszia_2-1738287863611.png

  • Business Rule TC article attached to incident
    • Have fixed the condition to say task.table == "incident"

Keszia_3-1738287931476.png

  • Script Action Articles attached to incident

Keszia_4-1738288012219.png

  • Email Notification

Keszia_5-1738288152110.png

 

When I Preview Notification, I can see a sys_id regardless of how many articles have been attached to the incident:

Keszia_7-1738288546576.png