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
Vaibhav BabrekarVaibhav Babrekar 

Please help me on following requirement

ABC Containers require the ability to automatically associate a Contact created in their Salesforce Instance with the respective Account based on the email domain specified in the primary email address of the Contact. The association should happen real time as soon as a Contact record is created within the system.
TODO
•Ensure all the discussed implementation best practices are followed.
•Develop the necessary piece of Apex code to implement a solution for the given problem statement.
•Develop the necessary Test code to attain the right code coverage (minimum 90% with at least 4 test cases)
Best Answer chosen by Vaibhav Babrekar
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vaibhav,

try with below code.

You can create Domain__c text field on account object and dont add this field in page layout.

Trigger:
trigger AssignAccountToContactBasedOnEmailDomain on Contact (before insert) {
    List<String> contactEmaildomains = new List<String>();
    List<account> insertacc = new List<account>();
    List<contact> conupdate = new List<contact>();
    for(Contact con : Trigger.new){
        If(con.email != Null ){
         contactEmaildomains.add(con.Email.split('@').get(1));  
        }

    }
    
    Map<string,string> mapcompanyname = new map<string,string>();
    for(string str: contactEmaildomains){
        mapcompanyname.put(str,str.substringBefore('.'));
    }

    if(contactEmaildomains.size()>0){
         for(string str: contactEmaildomains){
        Account acc= new account ();
             acc.Name = mapcompanyname.get(str);
             acc.Website = 'www.'+str;
             acc.Domain1__c = str;
             insertacc.add(acc);
         }
    }
    
    insert insertacc;
    
    List<Account> accList = [SELECT Id, website,Domain1__c FROM Account WHERE Domain1__c =:contactEmaildomains];

    Map<String, Id> domainsMap = new Map<String, Id>();
    for(Account a: accList) {
       domainsMap.put(a.Domain1__c, a.Id);
    }

    for(Contact c: Trigger.new) {
        if(domainsMap.containsKey(c.Email.split('@').get(1))) {
            c.AccountId = domainsMap.get(c.Email.split('@').get(1));
        } 
    }
    
}

Test Class:
@istest
public class AccountToContactBasedOnEmailDomain_Test {
    
    static testmethod void insertcon(){
        
        contact con = new contact();
        con.LastName = 'test con';
        con.email = 'test@vibhavcompany.com';
        insert con;
    }

}

If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Vaibhav,

When contact created and contact email matches with account email address then you need to link the contact to account?

Thanks!!
Vaibhav BabrekarVaibhav Babrekar
Hi Ankaish,
You are right because its given like "based on the email domain specified in the primary email address of the Contact". Can you please provide me the solution for this

Thanks in Advance
AnkaiahAnkaiah (Salesforce Developers) 
Can you clarity the  "based on the email domain specified in the primary email address of the Contact"??

The primary contact is linked to an account already??

Thanks!!

 
Vaibhav BabrekarVaibhav Babrekar
Hi Ankaiah,

If we enter the contact record with primary email as abc@tcs.com, abc@something.com then it should associate contact with Account as TCS or Something with website as www.tcs.com or www.something.com. And if account is not avalable for association then it should be created. Because its mentioned there "The association should happen real time as soon as a Contact record is created within the system".

Thanks in Advance
AnkaiahAnkaiah (Salesforce Developers) 
Thanks for the clarification.

primary email  is a custom field on contact? and domain name is available on website field of an account object? 

Thanks!!

 
Vaibhav BabrekarVaibhav Babrekar
No it's an email field on Contact object. Domain name is not available in website field, it should be automatically created by splitting email before @ symbol and appending www. to it. Such as abc@tcs.com then Account name will be TCS and Website will be www.tcs.com
AnkaiahAnkaiah (Salesforce Developers) 
Ok.
You have the website value " www.tcs.com" on account record right??

Thanks!!
Vaibhav BabrekarVaibhav Babrekar

No we don't have it. It's blank actually, programmatically we have to fill it in website field on Account object.

suppose I am creating contact whose email is abc@tcs.com then Account of TCS should be created with name "TCS"  in association with that contact and website field should be filled with www.tcs.com.

Thank you

AnkaiahAnkaiah (Salesforce Developers) 
Hi Vaibhav,

try with below code.

you can create Domain__c text field on account object and dont add this field in page layout.
 
trigger AssignAccountToContactBasedOnEmailDomain on Contact (before insert) {
    List<String> contactEmaildomains = new List<String>();
    List<account> insertacc = new List<account>();
    List<contact> conupdate = new List<contact>();
    for(Contact con : Trigger.new){
        If(con.email != Null ){
         contactEmaildomains.add(con.Email.split('@').get(1));  
        }

    }
    
    Map<string,string> mapcompanyname = new map<string,string>();
    for(string str: contactEmaildomains){
        mapcompanyname.put(str,str.substringBefore('.'));
    }

    if(contactEmaildomains.size()>0){
         for(string str: contactEmaildomains){
        Account acc= new account ();
             acc.Name = mapcompanyname.get(str);
             acc.Website = 'www.'+str;
             acc.Domain1__c = str;
             insertacc.add(acc);
         }
    }
    
    insert insertacc;
    
    List<Account> accList = [SELECT Id, website,Domain1__c FROM Account WHERE Domain1__c =:contactEmaildomains];

    Map<String, Id> domainsMap = new Map<String, Id>();
    for(Account a: accList) {
       domainsMap.put(a.Domain1__c, a.Id);
    }

    for(Contact c: Trigger.new) {
        if(domainsMap.containsKey(c.Email.split('@').get(1))) {
            c.AccountId = domainsMap.get(c.Email.split('@').get(1));
        } 
    }
    
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Vaibhav BabrekarVaibhav Babrekar
Hi Ankaiah,
Thanks alot for your help, can you please provide me the test code as given in the problem.

Thanks in Advance
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vaibhav,

try with below code.

You can create Domain__c text field on account object and dont add this field in page layout.

Trigger:
trigger AssignAccountToContactBasedOnEmailDomain on Contact (before insert) {
    List<String> contactEmaildomains = new List<String>();
    List<account> insertacc = new List<account>();
    List<contact> conupdate = new List<contact>();
    for(Contact con : Trigger.new){
        If(con.email != Null ){
         contactEmaildomains.add(con.Email.split('@').get(1));  
        }

    }
    
    Map<string,string> mapcompanyname = new map<string,string>();
    for(string str: contactEmaildomains){
        mapcompanyname.put(str,str.substringBefore('.'));
    }

    if(contactEmaildomains.size()>0){
         for(string str: contactEmaildomains){
        Account acc= new account ();
             acc.Name = mapcompanyname.get(str);
             acc.Website = 'www.'+str;
             acc.Domain1__c = str;
             insertacc.add(acc);
         }
    }
    
    insert insertacc;
    
    List<Account> accList = [SELECT Id, website,Domain1__c FROM Account WHERE Domain1__c =:contactEmaildomains];

    Map<String, Id> domainsMap = new Map<String, Id>();
    for(Account a: accList) {
       domainsMap.put(a.Domain1__c, a.Id);
    }

    for(Contact c: Trigger.new) {
        if(domainsMap.containsKey(c.Email.split('@').get(1))) {
            c.AccountId = domainsMap.get(c.Email.split('@').get(1));
        } 
    }
    
}

Test Class:
@istest
public class AccountToContactBasedOnEmailDomain_Test {
    
    static testmethod void insertcon(){
        
        contact con = new contact();
        con.LastName = 'test con';
        con.email = 'test@vibhavcompany.com';
        insert con;
    }

}

If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
Vaibhav BabrekarVaibhav Babrekar
Hi Ankaiah,
Thats awesome and thank you very much for your support.