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
Debendra Ray 8Debendra Ray 8 

Not Able to Update a Contract for an Account - It throws an error owing to recursion - Urgent Help Needed to resolve this recursion issue

Hi All,

I'm trying to update the owner of the Account and the corresponding Contract for this account, on completion of the approval process for the Contract - In doing so , while the owner of the Account gets updated successfully , however, the moment I try to update the corresponding Contract an error is thrown which shows a recursive call - Please find the code below :
public void autoTransferAgreementToSales(Contract quNew,Contract quOld ){
     
        List<Account> AccountToUpd = new List<Account>();
        List<Contract > ContractToUpd = new List<Contract >();
        AccountToUpd = [Select Id, OwnerId,Approval_Completion_of_Contract__c from Account where Id = :quNew.AccountId ];
        Map<Id,AccountHistory> history = new Map<Id,AccountHistory>();
        for(AccountHistory t : [Select Acct.AccountId,Acct.OldValue, Acct.NewValue, Acct.Field From AccountHistory Acct Where Acct.Field = 'Owner']){
          if(t.NewValue instanceof ID){
             history.put(t.AccountId, t);
             System.debug('Old: '+ t.OldValue +' New: ' +t.NewValue);
          }  
    
        }
        
        System.debug('DEBUG: autoTransferAgreementToSales::AccountToUpd ********'+ AccountToUpd.size() );
        System.debug('DEBUG: autoTransferAgreementToSales::AccountFldHistory ********'+ history.size() );
         
        for(Account acc: AccountToUpd ) {
           acc.Approval_Completion_of_Contract__c = true;
           if(history.containsKey(acc.Id)){
                 String accOwnerId = String.valueOf(history.get(acc.Id).OldValue);
                 System.debug('DEBUG: autoTransferAgreementToSales::accOwnerId ********'+ accOwnerId );
                 acc.OwnerId     = accOwnerId ;
                  ContractToUpd = [Select Id, OwnerId,Approval_Completion_of_Contract__c  from Contract
                                  where Id=:quNew.Id and AccountId = :quNew.AccountId];
                  for(Contract cont: ContractToUpd ) {
                               cont.Approval_Completion_of_Contract__c = true;
                               cont.OwnerId = accOwnerId ;
                               System.debug('DEBUG: autoTransferAgreementToSales::cont.OwnerId********'+ cont.OwnerId);                            
                   }
                 
           }         
                      
        }
        System.debug('DEBUG: autoTransferAgreementToSales::AccountToUpd ********'+ AccountToUpd.size() );          
        if(AccountToUpd.size() > 0)
              update AccountToUpd ;  
        
       
        System.debug('DEBUG: autoTransferAgreementToSales::ContractToUpd ********'+ ContractToUpd.size() );       
        if(ContractToUpd.size() > 0)
              update ContractToUpd ;         
        
     
     }

Error thrown is : Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Update failed. First exception on row 0 with id 8000k00000092CkAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SubmitForApprovalAgreement: maximum trigger depth exceeded Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck] Contract trigger event AfterUpdate for [8000k00000092Ck]: []".

Any help to resolve this recussion isssue will be much appreciated.

Thanks & Regards,

Debendra Ray
Best Answer chosen by Debendra Ray 8
Nithesh NNithesh N
Please Mark my Solution as Best Solution (Which Marks this Thread as Solved)
Let others find it for similar queries. 

All Answers

Nithesh NNithesh N
Hi Debendra, 

Use this Method to avoid recursion.
https://help.salesforce.com/articleView?id=000133752&type=1

Best,
Nithesh
Debendra Ray 8Debendra Ray 8
Hi Nithesh,
Thanks very much - It works.

Regards,

Debendra
Nithesh NNithesh N
Please Mark my Solution as Best Solution (Which Marks this Thread as Solved)
Let others find it for similar queries. 
This was selected as the best answer
Debendra Ray 8Debendra Ray 8
Done - Thanks again.