• Praharsh.Vasavda
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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) {} 
}