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
Sohel Shaikh 23Sohel Shaikh 23 

Account: execution of BeforeUpdate caused by: System.DmlException: Update failed.

Here two objects are used.Account and Schedule__c.
The main block's code is called by before trigger on account.
Trigger is on Account object.
It is not allowing me to update the Schedule__c object .

Please let me know why this error is encountered.


Error : 
Error:Apex trigger Account caused an unexpected exception, contact your administrator: Account: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0JM0000007bp55MAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Schedule: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a05M000000ACmimIAD; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 001M000000uSEuz) is currently in trigger Account, therefore it cannot recursively update itself: [] Class.ScheduleTrigger.updateCallsForSchedule: line 63, column 1 Trigger.Schedule: line 11, column 1: []: Class.AccountTrigger.NextCallCycleCheck: line 248, column 1

Main Block :
system.debug('Inside 6week block ');
                 a.REX_Call__c=false;
                 boolean acctNextCycle = a.Next_Cycle_Call__c;
                 List<Schedule__c> wk6Schedules = new list<Schedule__c>();

                 if(a.Priority_Cycle_Frequency__c == 'K: 6 Week Cycles: Weeks 1-2' || a.Priority_Cycle_Frequency__c == 'L: 6 Week Cycles: Weeks 3-4' || a.Priority_Cycle_Frequency__c == 'M: 6 Week Cycles: Weeks 5-6'){
                        system.debug('Inside 6week if block ');
                     for(Schedule__c schedule : [SELECT Id,Store__c,Week__c FROM Schedule__c WHERE Active__c = 'Y' AND Status__c = 'Active' AND Route__r.Status__c = 'Active' AND Store__r.Active__c = 'Y'  AND Store__c =: a.Id]){
                           system.debug('Inside schedule for loop ');
                        if (schedule.Week__c == 1 || schedule.Week__c == 2){
                               system.debug('Inside if block of week 1 and 2 ');
                            if (!acctNextCycle){
                                   system.debug('Inside nextcall true ');
                                reverseAcctNextCycleCall(a);
                            }else{
                                system.debug('Inside nextcall false ');
                                wk6Schedules.add(update6wkSchedule(schedule));
                            }
                        }else if (schedule.Week__c == 3 || schedule.Week__c == 4){
                            system.debug('Inside if block of week 3 and 4 ');
                            reverseAcctNextCycleCall(a);
                            wk6Schedules.add(update6wkSchedule(schedule));
                        }    
                     }
                     System.debug('all schedules to update are ...'+ wk6schedules);   
                     if (wk6Schedules.size() > 0) {
                         database.update(wk6Schedules);
                     }

Functions used in main block :
public static Account reverseAcctNextCycleCall(Account account){
         system.debug('Inside reverseAcctNextCycleCall ');
      Account schedAcct = new Account();
      schedAcct = account;
      if (schedAcct.Next_Cycle_Call__c){
        schedAcct.Next_Cycle_Call__c = false;
      }else{
        schedAcct.Next_Cycle_Call__c = true;
      }
        system.debug('New next cycle call :'+schedAcct.Next_Cycle_Call__c);
        system.debug('Exiting reverseAcctNextCycleCall');
      
      return schedAcct;
    }
    
    public static Schedule__c update6wkSchedule(Schedule__c sched){
    // if wk=1or2, +2 , if wk=3or4, -2
    system.debug('inside update6wkSchedule');
    Schedule__c schedule = sched;
    Decimal existingWk = schedule.Week__c;
    system.debug('Existing week : '+existingWk);
    Decimal newWk = 0;
    if (existingWk == 1 || existingWk == 2){
         newWk = existingWk + 2;
    }else if (existingWk == 3 || existingWk == 4){
         newWk = existingWk - 2;
    }else{
         return schedule;
    }
    schedule.Week__c = newWk;
        system.debug('New week : '+newWk+'schedule.Week__c : '+schedule.Week__c);
    return schedule;
  }


 
Best Answer chosen by Sohel Shaikh 23
Veenesh VikramVeenesh Vikram
Exactly, use future method to do the update and you are good to go!

Kindly mark solved if this resolves the issue.

Veenesh

All Answers

Veenesh VikramVeenesh Vikram
I guess in the After Update Trigger, you are calling an update on the same Record. That is why you are facing this error.
Try removing that update statement and update the record asynchronously.

Hope this helps.
Veenesh
Sohel Shaikh 23Sohel Shaikh 23
@Veenesh,

Could please tell me how to do it?
Shall I use @future to do it.

Thanks
Sohel 
Veenesh VikramVeenesh Vikram
Exactly, use future method to do the update and you are good to go!

Kindly mark solved if this resolves the issue.

Veenesh
This was selected as the best answer
Manmohan SinghManmohan Singh
Looks like you are trying to update a before trigger -   Account schedAcct = new Account(); 
All before triggers does not need additional update - just change the values and before trigger will take care of new values.
Remove all the update DML operations.
Sohel Shaikh 23Sohel Shaikh 23
I am trying to update the Schedule__C object which is in relationship with Account object.
Sohel Shaikh 23Sohel Shaikh 23
I used @future but the trigger on Account is getting fired again
Sohel Shaikh 23Sohel Shaikh 23
I used @future on separate function which is called by the trigger and this function updates Schedul__c object's records.
Account and Schedule__c are related.
Sohel Shaikh 23Sohel Shaikh 23
If I have master-detail realtionship between Account and Schedule__C objects then what would be the trigger sequence(Assume both objects have trigger) if I  update child record in master object's trigger?

Also please let me know where I could find concept related to this master detail relaionship inn terms of trigger execution.
What are DOs and DONTs for triggers in Master Detail relationship?
Sohel Shaikh 23Sohel Shaikh 23
Nope.
I just update the Schedule__c object
 
Sohel Shaikh 23Sohel Shaikh 23
I ma using Databse.update(list of type schedule__c)
Sohel Shaikh 23Sohel Shaikh 23
Yup I m able to do it using @future.
Thanks everyone.
Sohel Shaikh 23Sohel Shaikh 23
Not able to mark as solved?
Could you please help me in this