Add email to notification before sending

rishabhkata
Tera Contributor

I am having one requirement to add a email of a person to a notification before they are sent from ServiceNow instance. If there is new notification created then I don't want to add mail script to every notification and it wont be possible to add too add this user based on some condition. How can I add user to notification dynamically?

9 REPLIES 9

If all of your notifications share an email layout, you can place the mail script within the layout record. This will then be inherited. 

 

Otherwise a business rule would be needed on the sys_email table

Viraj Hudlikar
Giga Sage

Hello @rishabhkata 

 

How about writing a BR on sys_email table as configured below:

VirajHudlikar_0-1742396061531.png

and script something as below:

(function executeRule(current, previous /*null when async*/ ) {

    // Fetch the existing value from the "recipients" field
    var recipients = current.recipients;

    // Define the new emails to be added
    var newEmails = ['v@example.com', 'x@example.com'];

    // Check if the recipients field is not empty
    if (recipients) {
        // Split the existing recipients into an array
        var recipientArray = recipients.split(',');

        // Add the new emails to the array
        recipientArray = recipientArray.concat(newEmails);

        // Remove any empty strings from the array
        recipientArray = recipientArray.filter(function(email) {
            return email.trim() !== '';
        });

        // Join the array back into a string with comma separator
        recipients = recipientArray.join(',');
    } else {
        // If the recipients field is empty, just join the new emails
        recipients = newEmails.join(',');
    }

    // Update the "recipients" field with the new value
    current.recipients = recipients;

})(current, previous);

 

Note: Do test this and use this carefully as this will apply performance issue as it will be trigger on every email record sent out. Since you don't need to add through email script so this can be only approach, but test it thoroughly discuss with architect and then go with this over prod.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

@Viraj Hudlikar This won't work as email is already in the send queue and updating this record will only update recipient in our ServiceNow system but it won't trigger the notification to the user

@rishabhkata You can try to execute BR before insert data on this table which will be append your additional recipients as unless type changes emails are not triggered from system.

@Viraj Hudlikar I have already tried this same approach and user is not receiving any email but our servicenow is showing that it is sent to both users