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
S_BatmanS_Batman 

Converting Leads into Accounts based on custom fields

I have a custom field on the Lead level called Email Domain, as well as on the Account level.  
Can someone help me create a trigger, where any Leads that have Lead Status = Open are converted as Contacts into Accounts if the Email Domain field on the Lead and Account matches.

For Example;
Lead
Name - John Smith  
Lead Status - Open
Email - j.smith@abc.com
Email Domain - abc.com

Account 
Account Name - ABC
Email Domain - abc.com

Then the Lead John is converted into a Contact that belongs to Account ABC
Matthew CokeMatthew Coke
http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/

this should get you started. You only need to append your additional logic on the "if" line of the trigger. if you're trying to convert leads not only on insert but also on upate then you will need to alter the trigger accordingly
Dheeraj ShoppeDheeraj Shoppe
Hi!

Please check this code.
 
trigger LeadToContact on Lead (before insert, before update, after insert, after update) {   
    List<Lead> leadList = new List<Lead>();
    
    if(trigger.isBefore){
    	for (Lead l : trigger.New){
        	if(l.Status == 'Open - Not Contacted'){
            	if(l.Email_Domain__c != null){
                	leadList.add(l);
                 	System.debug('New leads having Email domain ### ' + leadList);
            	}
        	}
    	}
    
    	List<Account> accList = [select id, Email_Domain__c from Account where Email_Domain__c LIKE '%com'];        
    	List<Contact> contactList = new List<Contact>(); 	
   
    	for(Account acc :accList){
        	for(Lead lead : leadList)
            	if(acc.Email_Domain__c == lead.Email_Domain__c){
                	Contact c = new Contact(LastName = lead.LastName, AccountId = acc.Id);
                	contactList.add(c);
                    System.debug('Contacts to be created : ' + contactList);
                }
    	}
    
    	insert contactList;
    	}

}

Any suggestions.. appreciated :)