Skip to main content
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;

  }

 
12 answers
  1. May 23, 2016, 12:40 PM
    Exactly, use future method to do the update and you are good to go!

    Kindly mark solved if this resolves the issue.

    Veenesh
0/9000