Print this page

Owner Fields of Child Records Don't Auto-Update in Organization Sync

Knowledge Article Number 000213121
Description When a record’s owner field is updated in one organization, the update is applied to the linked organization in Organization Sync as expected. However, that record’s child entities, such as tasks and contacts, still list the former record owner in the linked organization until they are edited in the original organization.

For example, suppose Mark is working in the primary organization, and changes the owner of the Jamison Paper account from Alicia Harrison to Tom Smith. The Jamison Paper account in the secondary organization automatically updates to list Tom Smith as the owner. However, the Jamison Paper account has several child entities, such as tasks and contacts. The OwnerID field of these child entities will automatically update to list Tom Smith in the primary organization, but in the secondary organization, the child entities will still list Alicia Harrison as the owner.

A child entity’s owner is only updated in the secondary organization when a user makes any edit to the entity in the primary organization, triggering a sync of all of its fields. However, if the entity is edited in the secondary organization, the sync that it triggers will cause that entity in the primary organization to incorrectly list Alicia Harrison as the owner.
 
Resolution To make sure that the OwnerID field of child entities stays up-to-date, add an "after update" trigger that updates the OwnerID field of child entities to match their parent entity’s owner. The following trigger updates the OwnerID field of contacts when their parent account’s owner is updated. You can modify this trigger to account for all parent-child object combinations.
 
trigger updateContactOwner on Account (after update) {
    User user = new User();
    user = [SELECT Id, FirstName, LastName FROM User WHERE Id = :UserInfo.getUserId()];

    // Do the update only if this org is receiving updates from the publishing org
    // This is done by checking if the current user is the connection user if (user.FirstName.equals('Connection') && user.LastName.equals('User')) {
        List<Contact> updateContacts = new List<Contact>();

        for (Account oAccount : trigger.new) {
            List<Contact> contacts = new List<Contact>([SELECT Id FROM Contact where AccountId = :oAccount.Id]);

            for (Contact contact : contacts) {
                Contact oContact = new Contact();
                oContact.Id = contact.Id;
                oContact.OwnerId = oAccount.OwnerId;
                updateContacts.add(oContact);
            }

            // Update the contacts
            if (updateContacts.size() > 0) {
                Database.update(updateContacts);
                updateContacts.clear();
            }
        }
    }
}

 




promote demote