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

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;
Name - John Smith  
Lead Status - Open
Email -
Email Domain -

Account Name - ABC
Email Domain -

Then the Lead John is converted into a Contact that belongs to Account ABC
Matthew CokeMatthew Coke

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

Please check this code.
trigger LeadToContact on Lead (before insert, before update, after insert, after update) {   
    List<Lead> leadList = new List<Lead>();
    	for (Lead l : trigger.New){
        	if(l.Status == 'Open - Not Contacted'){
            	if(l.Email_Domain__c != null){
                 	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);
                    System.debug('Contacts to be created : ' + contactList);
    	insert contactList;


Any suggestions.. appreciated :)