+ Start a Discussion

Trigger recursion error

I am getting the following error:
QuoteTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q05D0000007jC3SAI; first error:

ELF_REFERENCE_FROM_TRIGGER, Object (id = 0Q05D0000007jC3) is currently in trigger QuoteTrigger, therefore it cannot recursively update itself: [] Trigger.QuoteTrigger: line 35, column 1

Trigger code is:
public static Boolean preventRecursive = true;
        preventRecursive = false;
        // Uncheck all latest quotes except for the one manually made latest 
        if(Trigger.isUpdate && Trigger.isBefore){
            if(Trigger.new[0].Latest_quote__c == true) { // proceed only if latest quote is checked
                List<Quote> qtList = [SELECT Id, opportunityId, Latest_quote__c 
                    FROM quote WHERE opportunityId IN :oppIds ORDER BY LastModifiedDate DESC];
                if (qtList.size() > 1) {
                    for(Integer i = 0; i < qtList.size(); i++){ // uncheck all from being latest
                        qtList[i].Latest_quote__c = false;
                qtList[0].Latest_quote__c = true; // mark only the manually marked as checked
                update qtList;

Line 35 is: update qtList;

Dino KozićDino Kozić

your list of quotes, qtList contains the record that's already in the trigger (Trigger.new[0]). Before update trigger prohibits updating the same record - instead you should change its field value. Please read through the following trailhead docs: https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro
Scrapped the trigger and used flow and it worked - flows rock!
Flow solution instead of a update trigger which is causing a recurssion error: https://youtu.be/RZOClRIpZeg