You need to sign in to do that
Don't have an account?

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
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
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:
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
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:
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.
Cheers
Thomas