Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Send Email Notifications Only to Addresses Ending with "@domain"?

12uy
Mega Sage

Hi everyone,

 

When sending notifications via email, in the 'Who will receive' section, I only want the notifications to be sent to email addresses ending with '@domain'.

 

Thanks!

 
1 ACCEPTED SOLUTION

12uy
Mega Sage

I tried Business rule and it worked well

 

var myRecipients = current.recipients;
var accDomain = "@domain"; // Change the domain to send
if(myRecipients.contains(accDomain)){  
	var emailArray = myRecipients.split(',');
	var accEmails = emailArray.filter(hasAccDomain);
	current.recipients = accEmails.join(",");
	current.update();
} else {
	current.setAbortAction(true);
}


function hasAccDomain(email) {
	return email.endsWith(accDomain);
}

 

 

View solution in original post

4 REPLIES 4

Priyanka0402
Mega Sage

Hello @12uy

You can use an email script to group users whose email addresses end with @domain in their email field. The following code snippet demonstrates how to achieve this: 

(function() {
    var recipients = [];
    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('email', 'ENDSWITH', '@domain'); // Filter users with emails ending with '@domain'
    userGR.query();

    while (userGR.next()) {
        recipients.push(userGR.email.toString());
    }
    
    // Set the recipients for the email notification
    email.setTo(recipients.join(','));
})();

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

Regards

Priyanka

I tried to insert the code into the email script, but it didn't work. Could you guide me through the steps?

 

Sid_Takali
Kilo Patron

Hi @12uy You can easily achieve your requirement through Email Address filter. Read below docs mentioned and refer below screenshots.

Sid_Takali_0-1722575086797.png

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0869547 

https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/notification... 

 

12uy
Mega Sage

I tried Business rule and it worked well

 

var myRecipients = current.recipients;
var accDomain = "@domain"; // Change the domain to send
if(myRecipients.contains(accDomain)){  
	var emailArray = myRecipients.split(',');
	var accEmails = emailArray.filter(hasAccDomain);
	current.recipients = accEmails.join(",");
	current.update();
} else {
	current.setAbortAction(true);
}


function hasAccDomain(email) {
	return email.endsWith(accDomain);
}