Skip to main content

#Trailhead  #Sales Cloud  #Service Cloud  #Formulas

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  

NOT(Conatins(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
  1. Dec 8, 2021, 8:37 PM

    Hi Shivangi,

     

    It is imperative that you understand how the CONTAINS function actually works:

    CONTAINS(text, compare_text)

    where compare_text is any fragment that should be found WITHIN the text argument.

     

    Basing off the logic you have written:

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

    You are expecting to find "@abc.com,@test.com, @test123.com" within the Email ID.

     

    So for example, if you have "Shivangi@abc.com" as your email ID, this formula is thinking 'will I find "@abc.com,@test.com, @test123.com" in the "Shivangi@abc.com" email ID?'

     

    The answer will 10000000000% be no all the time.

     

    You need to get clever in how you write this out based on your understanding of this function. Try this out:

    NOT(CONTAINS($Setup.Hierarchy_Setting__c.Domain__c, MID(Email_ID__c, FIND("@", Email_ID__c), 100)))

    This is essentially stripping out the domain in your Email_ID__c field and checking if that domain is in your list of domains.

     

    So back to the example of Email_ID__c being "Shivangi@abc.com" if we take the middle of it where it finds "@" you're left with "@abc.com"

    Notice I've reversed the domain and the Email ID as arguments in your function. Now this will assess 'will I find "@abc.com" within the string "@abc.com,@test.com, @test123.com"?'

     

    This should work.