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

@Keszia 

remember the above logic will send multiple emails for same incident if different articles are attached

Did you check my below comment where you can consolidate them together and send only 1 email per incident?

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar I want the email to be sent shortly after the incident has been resolved, rather than at a designated time each day.

@Keszia 

so 1 email for 1 incident and 1 article

Next email for same incident and 2nd article?

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Keszia 

Make these changes

1) business rule on m2m_kb_task with proper condition so that it triggers only for INC

Condition: current.task.number.toString().startsWith('INC')

Script:

(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(
            'incident.article.attached', // 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);

2) No script action required

3) Event on table "m2m_kb_task" -> which is already configured by you

4) Notification on table "m2m_kb_task" and associate the event you created with this notification

a) Event parm2 contains recipient checkbox true

b) Don't give anything in Users/Groups in fields as recipient is set from Business rule

Notification body as this

AnkurBawiskar_0-1738294111562.png

 

${mail_script:show_details}

5) new email script as this to show the list

(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);

Note: I believe I have provided enough guidance and you can take it further from here

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Keszia 

Hope you are doing good.

Did my reply answer your question?

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader