+ Start a Discussion
Travis Malle 9Travis Malle 9 

All accounts must have the same current owner and new owner

Hello Community,
 
I’m experiencing a strange issue that I’m not sure how to resolve. Any insights would be greatly appreciated…
 
I wrote some logic that is called by an after trigger on the Account object. The intent of my logic is to change the account owner of child accounts when the parent account is changed. This must only happen if a custom checkbox field is checked “Cascade_Ownership_Change__c” and if the trigger only contains one record (I don’t want this logic to run if there is a bulk change). Anyway, the logic works only when all of the child accounts are the same owner (not necessarily the same as the parent owner, just all the same person). If I try to perform an ownership change on a parent account where, for example, 1 child account out of 50 has a different owner. I get a Apex error stating the following: MasterTrigger_Account: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0016C000009UJLGQA4; first error: INVALID_FIELD, All accounts must have the same current owner and new owner.: [OwnerId] Class.Account_Utility.UpdateChildAccountOwnership: line 41, column 1 Trigger.MasterTrigger_Account: line 17, column 1
 
Here is what I’ve ruled out:
  • I do not have a validation rule preventing this update
  • Territory management does not have an effect on this logic
  • All applicable owners have a profile with read and edit permissions on all accounts
 
public void UpdateChildAccountOwnership(list<account> triggerNew, list<account> triggerOld){
        if(triggerNew.size() == 1 
           && triggerNew[0].Cascade_Ownership_Change__c == true
           && triggerNew[0].ownerId != triggerOld[0].ownerId
          )
        {
            id parentAcctId = triggerNew[0].id;
            id parentOwnerId = triggerNew[0].ownerId;
            list<account> childrenToUpdate= new list<account>();
            list<account> childAccts = [select id, parent_site__c, Name, ownerId from account where parent_site__c = :parentAcctId];
            
            for(account child : childAccts)
            {
                if(child.ownerId != null){
                    if(child.ownerId != parentOwnerId)
                    {
                        child.ownerId = parentOwnerId;
                        childrenToUpdate.add(child);
                    }                   
                }
            }
            
            update childrenToUpdate;
        }
    }
trigger MasterTrigger_Account on Account    (before insert, after insert, before update, after update, before delete, after delete) {
    /////* BEFORE TRIGGERS */////
    if (Trigger.isBefore)
    {
        if(Trigger.isInsert) {} 
        if(Trigger.isUpdate) {}
        if(Trigger.isDelete) {}
    }
    
    /////* AFTER TRIGGERS */////
    if (Trigger.IsAfter)
    {
        if(Trigger.isInsert) {} 
        if(Trigger.isUpdate) 
        {
            Account_Utility AcctUtil = new Account_Utility();
            AcctUtil.UpdateChildAccountOwnership(trigger.new, trigger.old);
            AcctUtil.CalculateEventScore(trigger.new, trigger.old);
            AcctUtil.CreateHistoryRecord(trigger.new, trigger.old);
        }
    } 
    if(Trigger.isDelete) {} 
}


 
Praharsh.VasavdaPraharsh.Vasavda
Hi Travis,

Did you get any resolution for this issue?
I am facing the same issue.

Thanks
Renato Matheus SimiãoRenato Matheus Simião
Hello guys,

This error also occurred to me, you only have to run the update DML once for each list of the same owner.
Note: Depending on the volumetry, it is recommended to send this process to a batch.
public void UpdateChildAccountOwnership(list<account> triggerNew, list<account> triggerOld){
    
    if(triggerNew.size() == 1 
       && triggerNew[0].Cascade_Ownership_Change__c == true
       && triggerNew[0].ownerId != triggerOld[0].ownerId
      )
    {
        id parentAcctId = triggerNew[0].id;
        id parentOwnerId = triggerNew[0].ownerId;
        list<account> childAccts = [select id, parent_site__c, Name, ownerId from account where parent_site__c = :parentAcctId ORDER BY ownerId];
        
        Map<Id,List<Account>> mAccount = new Map<Id,List<Account>>();

        for(account child : childAccts)
        {
            if(child.ownerId != parentOwnerId) continue;
            
            List<Account> lAccount;

            if (mAccount.containsKey(child.ownerId))
                lAccount = mAccount.get(child.ownerId);
            else lAccount = new List<Account>();

            child.ownerId = parentOwnerId;
            lAccount.add(child);
            mAccount.put(child.ownerId, lAccount);
        }
        
        for (List<Account> lAccount : mAccount.values())
            update lAccount;
    }
}