Sep 20, 2024
77 Views
Comments Off on Trigger to blacklist domain name for sending email
0 0

Trigger to blacklist domain name for sending email

Written by

You now have an awesome Salesforce instance that you use for your sales and marketing purposes. Your instance sends hundreds of mails weekly, many of them to prospective clients. But sometimes in a fast sales machine environment where your sales guys would be sending hundreds of bulk mails daily, it becomes very difficult to make sure that your sales agents do not send mails to some specific domains, or that they cross-check their lists every time from a common no-mailing lists.

You would come across cases like these in your business life-cycle, where you don’t want your sales prospectors to send mails to specific email addresses or even to specific domains, like unsubscribe markers, present clients, competitors, etc. Unfortunately it is very difficult to make sure through a process that your users don’t send mails to these domains, specifically for cases where your fellow SFDC users would be sending mails in bulk for marketing or prospecting.

The most simple solution is to make sure that your Salesforce instance itself is not able to send mails to said domains. Do this is not that difficult. You would just have to write a trigger.

In your salesforce org, if you want to blacklist domain name on which you don’t want to send mail from salesforce then you can use the following trigger to do this…

trigger BlackListEmailAddress1 on EmailMessage (before insert) {
    for(EmailMessage message: Trigger.New)
    {
        if((message.ToAddress.contains('gmail')) && message.Incoming == false )
        {
            message.addError('Email Alert: You have selected a receipient email from one of the blacklisted doamins please use another email address and try again');
        }
        if((message.ToAddress.contains('yahoo')) && message.Incoming == false )
        {
            message.addError('Email Alert: You have selected a receipient email from one of the blacklisted doamins please use another email address and try again');
        }
    }
}

In this trigger, we have blocked only two domains names Gmail and Yahoo. Your users won’t be able to send any mails that have Gmail or Yahoo in their domain name. If you need to block any specific ID or another domain, then all you have to do is add a new ‘if’ condition like

if((message.ToAddress.contains('example.com')) && message.Incoming == false )

This will work for all the objects where we have the option to send an email, like contacts, leads, custom objects, etc.

Article Categories:
Salesforce
Ramya Singh
https://thestarbiznews.com

Ramya Singh isn't your average tech blogger. Sure, she's got the brains to understand the latest algorithms and the jargon to explain them in plain English. But she's also got a twinkle in her eye and a way of weaving technology into the fabric of everyday life that makes it nothing short of fascinating. Whether she's reviewing the latest smartphone, exploring the potential of virtual reality, or delving into the ethical implications of artificial intelligence, Ramya does it with a contagious enthusiasm that makes you want to learn more, do more, and be a part of the exciting world of tech. So, if you're looking for a tech blog that's informative, inspiring, and just plain fun, follow Ramya Singh. She'll take you on a journey through the ever-evolving landscape of technology, and you might just find yourself a little bit more tech-savvy (and a lot more excited) along the way.