+ Start a Discussion
Thomas CailletThomas Caillet 

Trigger on Lead conversion

Hello community,

I try to code an apex trigger on sandbox based on lead conversion.

I explain to you : I have a lookup field to opportunities on my account page an I would like to put the opportunity converted on this field.
I try to make a process builder (doesn’t work) and a trigger when a lead is converted based on an other post :

trigger trigMapFields on Lead (before update) {
for(Lead lead:System.Trigger.new)
{
if (lead.IsConverted)
{
system.debug('');
Opportunity opp = [SELECT Id FROM Opportunity WHERE Opportunity.Id=:lead.ConvertedOpportunityId];
Account acc = [SELECT Id,Main_Account_Contact__c FROM Account WHERE Account.Id=:lead.ConvertedAccountId];
acc.Main_Opportunity__c = opp.id;
update acc;
system.debug('' + acc.Main_Account_Contact__c );
}
}}

But I think that lead.IsConverted doesn’t work.
I activated Use Apex Lead Convert which upon the conversion of leads, enforces required field settings, field validation rules, workflow actions in Lead settings.
Could you help me please. 

Thanks
Thomas
Best Answer chosen by Thomas Caillet
SidhantSidhant
Hey, Thomas -

I tried to understand your requirement and here is a sample way of doing this, assmunig only one opportunity is related to an account when a lead is converted.

You can try this code: 
trigger trigMapFields on Lead(after update) {
	map<String, String> accToOppMap = new map<String, String>();
	List<Account> accList = new List<Account>();
	// traverse on new list of leads 
	// and store accounts and oppurtunities, assuming one to one mapping
	for (Lead lead: Trigger.new) {
		if (lead.IsConverted) {
			accToOppMap.put(lead.ConvertedAccountId, lead.ConvertedOpportunityId);
		}
	}
	
	// query on account records which were related to leads 
	// and populate Opportunity field with associated Opportunity found on lead record
	for(Account accRecord : [SELECT Id, Main_Opportunity__c FROM Account WHERE ID IN: accToOppMap.keySet()]) {
		accRecord.Main_Opportunity__c = accToOppMap.get(accRecord.Id);
		accList.add(accRecord);
	}
	
	//update the account records
	try {
		update accList;
	}catch(Exception ex) {
		system.debug('Exception found: '+ex);
	}
}

Some suggestion for your code, these are sins in Apex programming
  • Never write a query in for loop
  • Never call DML statements in for loop
Let me know in case of concerns. I hope my sample helps you.

Best,
Sid

If this helps you please mark it as solved.

All Answers

SidhantSidhant
Hey, Thomas -

I tried to understand your requirement and here is a sample way of doing this, assmunig only one opportunity is related to an account when a lead is converted.

You can try this code: 
trigger trigMapFields on Lead(after update) {
	map<String, String> accToOppMap = new map<String, String>();
	List<Account> accList = new List<Account>();
	// traverse on new list of leads 
	// and store accounts and oppurtunities, assuming one to one mapping
	for (Lead lead: Trigger.new) {
		if (lead.IsConverted) {
			accToOppMap.put(lead.ConvertedAccountId, lead.ConvertedOpportunityId);
		}
	}
	
	// query on account records which were related to leads 
	// and populate Opportunity field with associated Opportunity found on lead record
	for(Account accRecord : [SELECT Id, Main_Opportunity__c FROM Account WHERE ID IN: accToOppMap.keySet()]) {
		accRecord.Main_Opportunity__c = accToOppMap.get(accRecord.Id);
		accList.add(accRecord);
	}
	
	//update the account records
	try {
		update accList;
	}catch(Exception ex) {
		system.debug('Exception found: '+ex);
	}
}

Some suggestion for your code, these are sins in Apex programming
  • Never write a query in for loop
  • Never call DML statements in for loop
Let me know in case of concerns. I hope my sample helps you.

Best,
Sid

If this helps you please mark it as solved.
This was selected as the best answer
Thomas CailletThomas Caillet
Thanks SidhantAgarwal for the trigger and explanations :)
Cheers
Thomas