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 there
s 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 =:
ct.AccountId].OwnerId;
ContactShare cs = new ContactShare();
cs.ContactId = ct.id;
cs.UserOrGroupId = accOwnerId;
cs.ContactAccessLevel = 'All';
cs.RowCause = 'Owner';
upCt.add(cs);
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';
upCt.add(cs2);
system.debug('CS2 ==>'+cs2);
}
}
}
system.debug('LIST -->'+ upCt);
insert upCt;
} // end trigger