I am trying to cascade the same ownerid across all contact owners within an Account Contact Relation hierarchy. I wrote a pretty basic after update trigger on an account to accomplish this.
When all records are owned by the same person, it updates correctly. When there is a mix of ownerids, I receive "INVALID_FIELD, All accounts must have the same current owner and new owner.: [OwnerId]" Please note: Person accounts are enabled in this Org.
Basic Logic
list<contact> Contacts = new list<contact>();
for(AccountContactRelation r: [select id,
AccountId,
contactid,
contact.ownerid,
contact.owner.Name
from AccountContactRelation
where AccountID in: AccountOwnerMap.keyset()
]){
system.debug('r: ' + r);
if(r.AccountID!=null && AccountOwnerMap.containskey(r.AccountID)){
Contact c = new Contact(id=r.Contactid);
c.ownerid = AccountOwnerMap.get(r.AccountID);
Contacts.add(c);
}
}
system.debug('Contacts: ' + Contacts);
//Update Contacts
if(Contacts.size()>0){
update Contacts;
}
Similar issue with no clear answers below.