You need to sign in to do that
Don't have an account?
Nitin Sharma
How to share a record with apex.?
scenario: owner of the record has changed to someone and get Read access to previous owner. My code below:
trigger ShareRecord on Opportunity (after update) {
List<Opportunity> opptylist = new List<Opportunity>();
for(Opportunity oppty : trigger.new){
if(oppty.OwnerId != trigger.oldMap.get(oppty.Id).OwnerId){
OpportunityShare opptyshare = new OpportunityShare();
opptyshare.UserorGroupId = trigger.oldMap.get(oppty.Id).OwnerId;
opptyshare.ParentId = oppty.Id;
opptyshare.AccessLevel = 'READ';
opptylist.add(opptyshare);
}
}
insert opptylist;
}
but didn't work.
Thanks,
Nitin
trigger ShareRecord on Opportunity (after update) {
List<Opportunity> opptylist = new List<Opportunity>();
for(Opportunity oppty : trigger.new){
if(oppty.OwnerId != trigger.oldMap.get(oppty.Id).OwnerId){
OpportunityShare opptyshare = new OpportunityShare();
opptyshare.UserorGroupId = trigger.oldMap.get(oppty.Id).OwnerId;
opptyshare.ParentId = oppty.Id;
opptyshare.AccessLevel = 'READ';
opptylist.add(opptyshare);
}
}
insert opptylist;
}
but didn't work.
Thanks,
Nitin
I doubt that your code was even compiling. Please try below code and let me know if it works. Thanks.
while using ur code through same error
line no 6 - Field not writeable: OpportunityShare.UserorGroupId