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

Hi @Ankur Bawiskar 

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.

@Keszia 

Did you follow all the steps?

Did you debug the components?

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

@Ankur Bawiskar yes I followed all the steps. I don't know what you mean by debug the components, sorry.

@Keszia 

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?

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

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
NameTC article attached to incident
Tablem2m_kb_task
When to runInsert

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
Nametc.articles_attached_to_incident
Tablem2m_kb_task
Queue/Script(none)

Fired by

TC article attached to incident

Script

(see below)

 

 Email Notification
NameAttached TC article email notification
Tablem2m_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
Nametc_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);