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
Mario EMario E 

How to convert leads when external data source email matches?

I have data from external data warehouse pushing data through Skyvia to Salesforce. It will upsert Contacts. However, I want to know how to have the process check if there is any existing lead with same email then convert that lead to a Contact then update that same Contact. 

Right now, our salespeople have to keep manually check if the Contact got created in Salesforce then they will need to go to the lead and manually convert and then choose the matching Contact. This needs to (and I know it can be) automated!

 

How can I achieve that? 

Sitarama MurthySitarama Murthy
Hi Marlo,

Use Apex Trigger and Class programming check if the newly created Lead already a Contact in SFDC, if Yes, pass that Lead ID's to ConvertLead method and map to existing Contact.

Thanks,
Ram
Dushyant srivastava 8Dushyant srivastava 8
Hi Mario,

To achieve this you have to write a trigger on Contact. Just for the reference Please see the code witten bellow:
 
trigger contactTrigger on Contact (after update , after insert) {
	if(Trigger.isAfter)
	{
		ContactTriggerHandler.onAfter(Trigger.new , Trigger.oldMap<Id , Contact>);
	}
}

Class ContactTriggerHandler
{
	public static void onAfter(List<Contact> lstContact , Map<Id , Contact> mapOldContact)
	{
		Map<String , Id> mapContactIds = new Map<String , Id>();
		
		for(Contact objContact : lstContact)
		{
			if(mapOldContact.containsKey(objContact.Id) && mapOldContact.get(objContact.Id).email__c != objContact.email__c)
			{
				mapContactIds.add(objContact.email__c , objContact.Id);
			}
			else if(objContact.email__c != Null)
			{
				mapContactIds.add(objContact.email__c , objContact.Id);
			}
		}
		
		List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
		for(Lead objLead : [Select Id, email from Lead where email In: mapContactIds.keySet()])
		{
			Database.LeadConvert objLeadConvert = new Database.LeadConvert();
			objLeadConvert.setLeadId(myLead.id);
			objLeadConvert.setContactId(mapContactIds.get(objLead.Id));
			leadConverts.add(objLeadConvert);
		}
		
		Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
	}
}

 
Mario EMario E

Thank you, Dushyant srivastava 8.

 

I am getting an error: 

force-app/main/default/triggers/custom_leadConversion.trigger  
Unexpected token ')'. (4:75)
The error is on line 4. I am not sure how to fix that as the parathesis looks right...
Dushyant srivastava 8Dushyant srivastava 8
Hi Mario,

Please Change the line to "ContactTriggerHandler.onAfter(Trigger.new , Trigger.oldMap);"
Mario EMario E

it is throwing error on tempList because it is nowhere to be found in the code.

 

I am getting an error after trying to insert Contacts via dataloader.io into a sandbox (in our Prod org, our external data warehouse is being pushed through a third party ETL tool into the org so Contacts get upserted):

execution of AfterInsertcaused by: System.NullPointerException: Attempt to de-reference a null object()


Unable to find out what is causing this error.