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

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([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 ([0]). Before update trigger prohibits updating the same record - instead you should change its field value. Please read through the following trailhead docs:
Scrapped the trigger and used flow and it worked - flows rock!
Flow solution instead of a update trigger which is causing a recurssion error: