Adding 'To' Recipients to the Notification through Email Script

lakshmi2002
Tera Contributor

Hi All,

I am working on a requirement related to Notification Email Scripts and need some guidance.

While configuring the notification, I attempted to use email.addAddress() to dynamically add recipients. It works as expected for cc and bcc, but adding recipients with 'to' is not functioning properly.


Requirement:

Whenever a task is created in a custom table (which extends the Task table), a notification is triggered. The expected behavior is:

  • If "Assigned to" is empty and "Assignment group" is populated, the notification should be sent to all members of the assignment group.
  • If "Assigned to" is populated, the notification should be sent only to that "Assigned to" user.

Constraints:

  • I want to achieve this using a single notification (by avoiding the creation duplicate notifications).
  • I do not want to use additional Business Rules or event-based notifications for this logic.

 

Is there a way to dynamically control the recipient (To field) within the Notification Email Script itself or through notification configurations to meet this requirement?

Please help me with this requirement.

Thanks in advance!

 

2 REPLIES 2

Yaramala
Mega Sage

Hi @lakshmi2002 

Hi Lakshmi,

For this requirement, I would not rely on email.addAddress('to', ...) inside a Notification Email Script.

email.addAddress() is mainly supported for adding cc and bcc recipients from a mail script. It is not the right/supported approach to dynamically control the main To recipients.

With your constraints, using one standard notification without event logic or a Business Rule makes this difficult because the “Who will receive” section is not conditional at the individual recipient-field level.

Best practice would be one of these options:

  1. Use two notifications with mutually exclusive conditions

Notification 1:
Condition: Assigned to is not empty
Recipient: Assigned to

Notification 2:
Condition: Assigned to is empty AND Assignment group is not empty
Recipient: Assignment group

This is the cleanest and most supportable approach. It is not really duplicate logic if the conditions are mutually exclusive.

  1. Use event-based notification

If you want one notification, trigger an event and pass the calculated recipient list in parm1/parm2, then enable “Event parm contains recipient” on the notification. This gives better dynamic control, but it requires event logic.

  1. Use single notification only if you accept CC/BCC

A mail script can dynamically add group members to CC/BCC, but that will not meet the requirement if they must be in the To field.

So the direct answer is:

No, there is no clean supported way to dynamically switch the To recipients from Assigned To to Assignment Group members only through a Notification Email Script.

For a supportable implementation, I would use two mutually exclusive notifications, or move to an event-based notification if one notification is mandatory.

Also, if sending to an assignment group, make sure the group/member notification behavior is configured correctly and the users have valid email addresses and notifications enabled.

Please mark this as Helpful if it helps.

If this addresses your question, please mark this response as Accepted Solution 

Yaramala_0-1781021383956.png

 

 

 

 or mark has helpful 

Yaramala_1-1781021383957.png

 

 

 

 if you find it helpful.

Thank you!

TejaswiniY

 

Tanushree Maiti
Tera Patron

Hi @lakshmi2002 

 

You can use a mail script to dynamically route the email. In the notification's Who will receive tab, assign it to both the Assigned to user and the Assignment group, and check Event parm 1 contains recipient .

 

  1. Create the notification
  • Navigate to System Notification > Email > Notifications.
  • When to send: Set Record inserted (and/or updated) on your custom table.
  • Condition: Create your trigger conditions (as per your requirement)
  • Who will receive: Under Users/Groups in fields, select Assigned to and Assignment group.
  • Advanced View: Check the box for Event parm 1 contains recipient

 

  1. Create Notification Email script
  • Navigate to System Notification > Email > Notification Email Scripts>Click New
  • create an email script named task_notification

 

(function runMailScript(current, template, email, event, email_action) {

       if (!current.assigned_to.nil()) {

        email.addAddress("to", current.assigned_to.email, current.assigned_to.getDisplayValue());

    }

    else if (current.assigned_to.nil() && !current.assignment_group.nil()) {

        var gr = new GlideRecord("sys_user_grmember");

        gr.addQuery("group", current.assignment_group);

        gr.query();

        while (gr.next()) {

            if (gr.user.email) {

                email.addAddress("to", gr.user.email, gr.user.getDisplayValue());

            }

        }

    }

})(current, template, email, event, email_action);

 

Step 3: Call the Script in the Notification Body

Add the email script to the Message HTML field
${mail_script:task_notification}

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti