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

Help needed with recursion in trigger - lead conversion
I have a use case where, when a lead is converted, I need to convert all other leads from the same company. I have a custom object (Lead Company) that the leads are linked to via Lookup. The Lead Company has a flag Converted__c which is set when a lead is converted, from a lead <before update> trigger and the Lead Company record also then records the Account Id of the converted lead. There is then a trigger on the Lead Company which detects that the flag has been set and then converts all other leads on that account to contacts. Or at least, that is what is intended, but I get an error saying that it is recursive, as the conversion process then calls my lead trigger again.
So, I understand where the error is coming from, but what I can't work out is how to achieve this objective without the recursion.
Can anyone help me with a better solution?
Thanks
I have fixed this. For reference for anyone hitting the same issue, I'm explaining the solution here.
It was caused by my picking up the current lead, which was in the process of being converted, in the list of leads related to the lead company that needed to be converted. This was because the IsConverted flag had not yet been committed to the database so wasn't being filtered out of my query. I solved it by putting a static set of Ids into my outer class. When converted a lead I added its id to the set and then excluded this set from the query of leads to be converted, thus solving the recursion.
List <Lead> LeadLst = [Select Id, FirstName, LastName, Lead_Company__c, Email, Country, Company, IsConverted From Lead
where Lead_Company__c in :LCSet and IsConverted = false and Id not in :LeadCompanyUtl.LeadBeingConverted];
All Answers
Hi,
Can you post your trigger code?
I can, but I don't think the code is the issue, it is the design that is the problem, i.e:
1. A lead trigger updates a checkbox in a related object
2. Trigger in related object then tries to update other leads (to convert them)
3. This causes the trigger in (1) to fire again, which throws a fatal recursion exception.
I'm asking if there is any other way to do this. The only way the leads in (3) would know that the lead in (1) was converted is via the related Lead Company record in (2). However, this appears to be forbidden in salesforce because it chains all the triggers.
Lead Company trigger (had to comment it out for now so I can test the rest of the functionality):
Outer class:
Hi,
I think you can break the recursion. Do the following:
1) Create a Class with static variable (preferably boolean) and initialize it to true in the class;
2) Check this varaible in the lead trigger before doing any operation.
3) Set the value of the static variable as false in LeadCompany trigger before invoking "LeadCompanyUtl.convertLead(LeadLst, AccMap)".
At this point the lead trigger fires upon conversion but it checks the static variable and does nothing. This will break the recursion.
4) Change back the value of the static variable as true, if it is required to run the same lead trigger but does not lead to recursion.
This is suggested based on the fact that the static variable retains the value, changed anywhere, in one apex transaction. The recursion is a part of the apex trigger transaction.
Hope this helps.
I already had such a variable and was checking it, but put in an extra check as the first line to be sure. However, that is not the issue. I wasn't worried about the actual recursion, I was worried about the exception thrown. The error report is:
I don't believe that I am trying to convert the lead that has already been converted - checking the log, that is not the id reported - and I am checking the boolean and exiting if it is being called from the ConvertLead outer class. So I'm stuck here. :-(
I have fixed this. For reference for anyone hitting the same issue, I'm explaining the solution here.
It was caused by my picking up the current lead, which was in the process of being converted, in the list of leads related to the lead company that needed to be converted. This was because the IsConverted flag had not yet been committed to the database so wasn't being filtered out of my query. I solved it by putting a static set of Ids into my outer class. When converted a lead I added its id to the set and then excluded this set from the query of leads to be converted, thus solving the recursion.
List <Lead> LeadLst = [Select Id, FirstName, LastName, Lead_Company__c, Email, Country, Company, IsConverted From Lead
where Lead_Company__c in :LCSet and IsConverted = false and Id not in :LeadCompanyUtl.LeadBeingConverted];