I have Validation Rule that checks if entered Email ID has particular Domain.

If it does not contain any of these domains then validation should fire

I am storing Domains in a Hierarchy Custom setting like

Domain__c = @abc.com,@test.com,@test123.com

And then in the validation rule doing the following:

NOT(CONTAINS(Email_ID__c, $Setup.Hierarchy_Setting__c.Domain__c))

Its working only when Domain__c has single value(Domain__c = @abc.com), but not working with comma separated values.

I tried using colon like Domain__c = @abc.com:@test.com:@test123.com but no luck.

I also tried with custom labels.

Any other approach?

2 Answers 2


You could try flipping your logic to see if the custom setting contains the domain in the email.

Below, I'm looking in the custom setting for the domain (using LEN and RIGHT to strip out the domain).

NOT(Contains($Setup.Hierarchy_Setting__c.Domain__c, RIGHT(Email_ID__c, LEN(Email_ID__c) - FIND('@', Email_ID__c))

(Note: not tested, so you might need to adjust the logic for getting the domain part of the email address)


If you want to set up an allowlist validation (formerly termed whitelist), you will likely need to validate via Apex Trigger rather than Validation Rule, so you can iterate through your allowed values. Ideally you should combine them into a regex. Typically you would compile your allowlist something like:

List<String> allowedDomains = new List<String>();
for (My_Metadata__mdt record : My_Metadata__mdt.getAll())
String domainExpression = '.*(' + String.join(allowedDomains, '|') + ')';
Pattern domainPattern = Pattern.compile(expression);

You'll want to compile the above just once for your trigger context. Then you can iterate through your trigger records to enforce the rule.

for (MyObject__c record : newRecords)
    if (!domainPattern.matcher(record.Email_Id__c).find())
        record.addError('Domain not allowed');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .