+ Start a Discussion
Harsh DwivediHarsh Dwivedi 

getting an error in the trigger 'trigger cannot recursively update itself' ??

error :- First exception on row 0 with id 0012v00002J69maAAB; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0012v00002J69ma) is currently in trigger isdead, therefore it cannot recursively update itself: []: Trigger.isdead: line 43, column 1.

Code-
trigger isdead on Account (before insert,before update) {
    
    list <Account> up=new list<Account>();
    
    Set<Id> stdIds = new Set<Id>();
    for(Account ac : Trigger.new)
    {
        stdIds.add(ac.Id);
    }
    List<contact> contactlist = new List<Contact>();
        integer x=0;
        List<Account> Act =[select id from Account where Id in :stdIds];
        System.debug(Act);
        for(Account A : Act)
        {
           contactlist=[select Id,Dead__c from Contact where AccountId =:A.Id];
            x=0;
           system.debug(contactlist);
            for(Contact c:contactlist){
                if(c.Dead__c==true){
                    system.debug('here');
                    x++;
                }
            }
            
            system.debug('value of x='+x);
            system.debug(contactlist.size());
            
            if(x>contactlist.size()){
                A.need_intel__c=true;
            
            }
            else{
                A.need_intel__c=false;
                
            }
 
            up.add(A);

            
        }
  
      ------->  update up; // code gets stuck at this point cannot really                                              figure it out why 
       
        
    }
GrumpyDevGrumpyDev
Hi, 
In just a quick Overview..
You dont need to use Update while in before trigger.

Did you check to see if your code is working when you remove the update line?
"update up; "
Ajay K DubediAjay K Dubedi
Hi Harsh,

Try the following code it may be helpful for you:
trigger isdead on Account (before insert, before update) {
    
    Set<Id> stdIds = new Set<Id>();
    integer x=0;
    for(Account ac : Trigger.new)
    {
    x=0;
        List<contact> contactlist = new List<Contact>();
        contactlist=[select Id,Dead__c from Contact where AccountId =:ac.Id];
        if(contactlist.size()>0){
        for(contact con:contactlist){
        if(con.Dead__c==true){
            x++;
        }
    }
    }
     if(x>contactlist.size()){
                ac.need_intel__c=true;
            }
            else{
                ac.need_intel__c=false;
            }
    }
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi