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
} // end trigger