I made a trigger to insert new contact share (user) if the account owner is changed.. and I want to insert new contact share without removing all previous contact share (old account owner/contact owner).. When user change the account owner, it will set the new owner for contact ( account owner == contact owner )..

But everytime I changed the account owner, theres no log in dev console about my trigger, and theres only account owner (user) in contact share, all previous contact share is gone..

 trigger AutoShareContact on Contact (before update) {
   List<ContactShare> listContactShare = [SELECT Id, UserOrGroupId, 
                                           ContactAccessLevel, ContactId 
                                          FROM ContactShare];
    List<ContactShare> upCt = new list<ContactShare>();
    for (Contact ct : Trigger.new){
        String accOwnerId = [SELECT Id, OwnerId FROM Account WHERE Id =: 

        ContactShare cs = new ContactShare();
        cs.ContactId = ct.id;
        cs.UserOrGroupId = accOwnerId;
        cs.ContactAccessLevel = 'All';
        cs.RowCause = 'Owner';
        system.debug('CS ==>'+cs);

        for (ContactShare cShare : listContactShare){
            if (cShare.ContactId == ct.id){
                ContactShare cs2 = new ContactShare();
                cs2.ContactId = ct.id;
                cs2.UserOrGroupId = cShare.UserOrGroupId;
                cs2.ContactAccessLevel = 'Edit';
                cs2.RowCause = 'Manual';
                system.debug('CS2 ==>'+cs2);
     system.debug('LIST -->'+ upCt);
     insert upCt;
    } // end trigger

1 Answer 1


You have to write trigger on Account instead of Contact. Since, Account owner field is changed, it won't execute contact trigger. You should write an after update trigger on Account which checks if old value of account owner does not match with new value of account owner, then insert a ContactShare record

  • now I encountered another error said INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] .. what does this mean? May 10, 2019 at 4:26
  • 1
    It means that User are accessing a record or a field which is not accessible to the User. It can be Owner record, Account record or Any field which is hidden due to FLS May 10, 2019 at 5:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .