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
Akash Choudhary 17Akash Choudhary 17 

I want to move new contacts to the matching accounts based on domain. Contact Email should match the account's domain

Hi all,

Here is my code though its not showing any error but I am certain there is someting wrong with it. As it is not fulfilling the requirement.
Please help me rectifying it. and let me know where I am goung wrong.

public class Move_Contact {
    public static void updateContact(List <Contact> conList){
        String MainDomain = '';
        string website ='www' +MainDomain;
                string httpWebsite ='http://www' + MainDomain;
                string httpswebsite ='https://www'+ MainDomain;
                string international = MainDomain + '.%';
        for(Contact myCon : conList){
            if(myCon.Email != null){
                string domain = myCon.Email.split('@').get(1);
                MainDomain = domain;
            }}
                Map <Id,Id> conMap = new Map <Id,Id>();
        for(Contact myCon : conList){
            conMap.put(myCon.Id , mycon.AccountId);
        }
        
                List<Account> acc = [SELECT Id
                                     FROM Account 
                                     WHERE Website =:website
                                     or Website =:httpWebsite
                                     or Website =:httpswebsite
                                     or Website =:international];
                if(acc.size() == 1){
                    For(Contact c : conList){
                        acc.get(0).Id = conMap.get(c.Id);
                        c.AccountId = conMap.get(c.Id);
                    }
                }     
    }

}
imrohitimrohit
hey Akash
Check your account list in system.debug to make sure whether  your query returning records or not because i think you are making mistake in assinging values to  website,httpwebsite,httpswebsite and international variables  and also you are not updating  contact list after  assigning accountid to it...
Raj VakatiRaj Vakati
Please refer this link you have a solution ..

https://salesforce.stackexchange.com/questions/70539/assign-account-to-contact-based-on-email-domain


Here is the sample code .. bulkfy this code pls 
 
trigger cnt on Contact (before insert) {
Move_Contact.updateContact(trigger.new);

}
 
public class Move_Contact {
    public static void updateContact(List <Contact> conList){
        Map <Id,Id> conMap = new Map <Id,Id>();
        
        for(Contact myCon : conList){
            if(myCon.Email != null){
                string domain = myCon.Email.split('@').get(1);
                //MainDomain = domain;
                String MainDomain = domain;
                string website ='www' +MainDomain;
                string httpWebsite ='http://www' + MainDomain;
                string httpswebsite ='https://www'+ MainDomain;
                string international = MainDomain + '.%';
                //   conMap.put(myCon.Id , mycon.AccountId);
                
                Account acc = [SELECT Id
                               FROM Account 
                               WHERE Website =:website
                               or Website =:httpWebsite
                               or Website =:httpswebsite
                               or Website =:international Limit 1];
                
                myCon.AccountId = acc.Id ;
            }
        }
        
       
    }
    
}



 
Steven NsubugaSteven Nsubuga
Based on your code, here are my modifications. In your Salesforce instance, create a formula field called Website_Domain_Name__c to show the Domain name from the Account website field.

The formula.
SUBSTITUTE(Website, LEFT(Website, FIND(".", Website)), NULL)

The Apex code
public class Move_Contact {
    public static void updateContact(List <Contact> conList){
               
		Map <Id, Contact> conMap = new Map <Id, Contact>();		
		Map <String, List<String>> domainConMap = new Map <String, List<String>>();
		
		Set<String> sites = new Set<String>();
		List<String> domainCons = new List<String>();
		
        for(Contact myCon : conList){
			String MainDomain = '';
			conMap.put(myCon.Id , myCon);
            if(myCon.Email != null){
                string domain = myCon.Email.split('@').get(1);
                MainDomain = domain;
				domainCons.add(MainDomain + '' + myCon.Id);
				sites.add(MainDomain);
            }
		}
		
		for(String domainCon : domainCons){
			List<String> cons = domainConMap.get(domainCon.split(',')[0]);
			if (cons == null) {
				cons = new List<String>();
			}
			cons.add(domainCon.split(',')[1]);
			domainConMap.put(domainCon.split(',')[0], cons);
		}
		
		// Website_Domain_Name__c is a custom formula field on Account that extracts the Domain_Name from the Account's website field
		List<Account> acc = [SELECT Id, Website_Domain_Name__c, Website
							 FROM Account 
							 WHERE Website IN:sites];
							 
		if(acc.size() > 0){
			List <Contact> contactList = new List <Contact>();
			For(Account ac : acc){
				
				
				List<String> cons = domainConMap.get(ac.Website_Domain_Name__c);
				for(String con : cons){
					Contact c = conMap.get(con);
					c.AccountId = ac.Id;
					contactList.add(c);
				}
			}
			update contactList;
		}     
    }
}

 
Akash Choudhary 17Akash Choudhary 17
Hi Raj v,

Your is working but when I am bulkiying this, then its not . please help 

Hi Steven,

I wanted to do this without any custom field but thanks anyway
sgsssgss
Can you provide the code to assign contact to account based on domain of Email(Contact) and Website(Account)without using Formula Field