+ Start a Discussion
sumit suwalkasumit suwalka 

Error:-SELF_REFERENCE_FROM_TRIGGER

Hi All,
       i have a trigger on Account which is giving me error:-Trigger_Account: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 1 with id 0010o00002cFEGpAAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0010o00002cFEGp) is currently in trigger Trigger_Account, therefore it cannot recursively update itself: [] Class.AccountTriggerHelper.getCredebilityOfParentAcc: line 73, column 1 Trigger.Trigger_Account: line 24, column 1
       *******
       trigger Trigger_Account on Account (before insert, before update, 
                                    before delete, after insert, 
                                    after update, after delete) {
                                         
    AccountTriggerHelper.newAccount = trigger.new;
    AccountTriggerHelper.oldAccount = trigger.old;
    AccountTriggerHelper.newMapAccount = trigger.newMap;
    AccountTriggerHelper.oldMapAccount = trigger.oldMap;         
                                        
                                        if(!AccountTriggerHelper.runTrigger) {
                                            return;
                                        }

                                        if(Trigger.IsBefore){
                                            if(Trigger.IsInsert){
                                                AccountTriggerHelper.updateOppAmountOnAccount();
                                                AccountTriggerHelper.getCredebilityOfParentAcc();
                                            }
                                        } 
                                        
                                        if(Trigger.IsBefore){
                                            if(Trigger.IsUpdate){
                                                AccountTriggerHelper.updateOppAmountOnAccount();
                                                AccountTriggerHelper.getCredebilityOfParentAcc();
                                            }
                                        } 
        
}
Account trigger helper:-
public class AccountTriggerHelper {
    
    public static List<Account> newAccount = new List<Account>();
    public static List<Account> oldAccount = new List<Account>();
    public static Map<Id, Account> newMapAccount = new Map<Id, Account>();
    public static Map<Id, Account> oldMapAccount = new Map<Id, Account>();
    
    public static boolean runTrigger = True;
    
    public static Map<ID,List<Opportunity>> MapAccIdToOpp = New Map<ID,List<Opportunity>>();
    
    public static void  updateOppAmountOnAccount(){
        
        Set<id> accids = new Set<id>();
        for(Account acc : newAccount){
            accids.add(acc.id);
        }
        
        for(Id accid : accids){
            List<Opportunity> Listopp = MapAccIdToOpp.get(accid);
            System.debug('List'+ MapAccIdToOpp.get(accid));
            MapAccIdToOpp.put(accid, Listopp);
            System.debug('MapAccidtoOpp'+MapAccIdToOpp);
        }
        
        List<Account> AccountListToUpdate = new List<Account>();
        
        for(Account acc : newAccount){
            Decimal Amount = 0;
            
            for(Opportunity opp :  [SELECT id ,Name,StageName,Amount FROM Opportunity WHERE AccountId =: acc.id] ){
                if(opp.StageName == 'Closed Won' || opp.StageName == 'Closed Lost'){
                    Amount = Amount + opp.Amount;
                    System.debug('AmountIfupdate'+Amount);
                }
                
                if(opp.StageName != 'Closed Won' && opp.StageName != 'Closed Lost'){
                    Amount  = Amount + (opp.Amount)*0.25;
                     System.debug('Amountelseupdate'+Amount);
                }
                
                acc.Opportunity_Amount__c = Amount;
                System.debug('Amountupdate'+acc.Opportunity_Amount__c);
                AccountListToUpdate.add(acc);
            }
            
            System.debug('acctoupdate'+AccountListToUpdate);
        }        
        
    }
    public static void getCredebilityOfParentAcc(){
        List<Account> accListToUpdate = new List<Account>();
        Integer Point = 0;
        Map<Account,Decimal> MapAccToAmount = new Map<Account,Decimal>();
        for(Account acc : [SELECT id ,Name,Opportunity_Amount__c  
                          FROM Account 
                          WHERE Parentid = null 
                          AND Opportunity_Amount__c <> null 
                          ORDER BY Opportunity_Amount__c desc]){
            MapAccToAmount.put(acc,acc.Opportunity_Amount__c);
            system.debug('mapAmount'+MapAccToAmount);
                              
        }
        for(Account acc : MapAccToAmount.keyset()){
            Point = Point+1;
             System.debug('point'+Point);
            acc.RankOfAccount__c = Point;
            accListToUpdate.add(acc);
        }
        System.debug('point'+  accListToUpdate);
        
            runTrigger = false;
            Update accListToUpdate;
            runTrigger = true;
         
         
    }
    
}
How to solve this issue? Any suggestions?
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Sumit

You are trying to update the same record that is already present in the trigger context.Which means the record of account is already in trigger and you are trying to update it once again in the getCredebilityOfParentAcc method.
Either you will have to skip the accounts already present in trigger.New or if the requirrement is to consider them then that part of code should be implemented in before update without DML action.

Cheers!!!