+ Start a Discussion
JIDzenpriseJIDzenprise 

Circular_Dependency???

I'm using the following code to grab the Partner on an Opportunity and populate a custom field on the Account called Partner Account. For some reason every once in a while it throws an error that states that there's a circular dependency. Can someone point me in the right direction to figure out how to fix this?

 

trigger PopulatePartnerOnAccountFromList2 on Opportunity (before update) {
// THIS TRIGGER WILL OVERWRITE ANY PARTNER DEFINED IN THE FIELD Partner ON THE ACCOUNT OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVOID DATA BEING OVERWRITTEN BY MISTAKE...

   for (Opportunity o : Trigger.new) {
  

 


       // CREATE ARRAY OF ALL PARTNERS ON THIS OPPORTUNITY. THE REASON WHY WE DONT PICK THE PRIMARY PARTNER ONLY
       // IS BECAUSE THE PRIMARY FLAG IS NOT ALWAYS SET WHEN PARTNERS ARE ADDED TO OPPORTUNITIES. ONLY WHEN SALES DOES TIS
       // MANUALLY FROM THE RELATED LIST IS PRIMARY CHECKBOX CHECKED...


       OpportunityPartner[] PartnerArray = 
       [select AccountToID, IsPrimary from OpportunityPartner where OpportunityId = :o.id ORDER BY isPrimary DESC, CreatedDate];
       if (PartnerArray.size() > 0) {
         
           // IF PRIMARY IS DEFINED THEN THIS WILL BE THE FIRST OBJECT. IF NOT THE FIRST ADDED PARTNER WILL BE ADDED...
                     List<Account> AccountArray = [select id,Partner_Account__c from Account where Id = :o.AccountId ];
           for (Account a: AccountArray ){
                       a.Partner_Account__c=PartnerArray[0].AccountToID;
            update a;
                       }
            
       }
   }
 }

 

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateAccountPartner caused an unexpected exception, contact your administrator: UpdateAccountPartner: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0014000000b5gNsAAI; first error: CIRCULAR_DEPENDENCY, attempt to violate hierarchy constraints: []: Trigger.UpdateAccountPartner: line 24, column 1

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sanjdevsanjdev

Hi,

 

It seems you are trying to create a circular account hierarchy,and the same is not allowed

 

For Example:       Account 1's parent is Account 2,

Account 2 parent is Account 3

and then you are trying to set Account 3 parent to be Account 1

 

 

Also i Can see that you are performing an DML operation inside for loop. this is not a best practice.

 

Hope this answer you.

 

Mark it as resolved if it helps you.