function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arjuna Rajput 29arjuna Rajput 29 

Extract domain from email field in contact and check if any account of same name exist. If yes link contact to that account.

Create a trigger on Contact which extract domain from email field and check if any account of same name exists.

eg
: -abc@gmail.com 
-after extract get gmail.
-then it check gmail field  e in account object
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Arjuna,

Once the domain matches with the Account name do we need to show some error message or any. Can you let us know  what should be the behaviour in this scenerio.

Thanks,
 
arjuna Rajput 29arjuna Rajput 29
if any account exists of same name then create relationship between contact and account
arjuna Rajput 29arjuna Rajput 29
yes if not matches show  error can you do using trigger design pattern
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Arjuna,

This is an example trigger which shows a validation if there is no matching account name with the contact domain .
 
trigger Domain on Contact (before insert,before update) {
    Set<String> emailDomains = new Set<String>();
    for(Contact c:Trigger.new){
        if(c.email!=null){
            
              String Domain=(c.Email.split('@').get(1)); 
            emailDomains.add(Domain.replace('.com',''));
            system.debug('domains'+emailDomains);
        }
    }
    Map<id,Account>mapacc= new Map<id,Account>([select id,Name from Account where Name in :emailDomains]);
    
    for(Contact c: Trigger.new){
        if(!mapacc.containsKey(c.AccountId)){
            c.adderror(' Please create the Account with the domain name');
        }
    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 
arjuna Rajput 29arjuna Rajput 29
we dont print this  c.adderror(' Please create the Account with the domain name');
we do this :
if domain name matches to account then make a link(relationship) between both object account and contact.
arjuna Rajput 29arjuna Rajput 29
hi @SaiPraveen

trigger Domain on Contact (before insert,before update) { Set<String> emailDomains = new Set<String>(); for(Contact c:Trigger.new){ if(c.email!=null){ String Domain=(c.Email.split('@').get(1)); emailDomains.add(Domain.replace('.com','')); system.debug('domains'+emailDomains); } } Map<id,Account>mapacc= new Map<id,Account>([select id,Name from Account where Name in :emailDomains]); for(Contact c: Trigger.new){ if(!mapacc.containsKey(c.AccountId)){ c.adderror(' Please create the Account with the domain name'); } } }

in this code we don't print any error message if domain name matches to account name just assign domain name=Account Name what result we got..
just checking
1:i create new account name jbl
2:then we create a new contact and when we fill email field like abc@jbl.com and we left account name field because when we created this contact what we see result domain name jbl value automatically filled in account name.