You need to sign in to do that
Don't have an account?
Can't remove a Sharing Reason via Apex
I have two Sharing Reasons on a custom object in a recruiting app.
If the candidate goes to "employee" status, i want to only share with the management group.
If the candidate goes to any other status, I will share with the employee group.
I have an after insert, update trigger on my candidate object. In the trigger I pull the existing shares like this:
List <T7Candidate__Share> candidateShares = [SELECT Id, ParentId, UserOrGroupId,AccessLevel,RowCause FROM T7Candidate__Share];
Then I remove any exisitng Sharing Reasons (except for 'Owner'). To bring the sharing rules back down to Owner.
Then I generate a new Sharing Reason and append it to the share list and push it back up with:
Database.Saveresult[] canShareInsertResult = Database.insert(candidateShares,false);
I'm debugging the entire process and here's what I see:
1. Step one: I change the candidate status to a non-employee status and the Employee Share Reason is generated fine.
2. Step two: I change the candidate status to employee and the Management Share Reason replaces the Employee Share Reason when I inspect the Debug Logs
3. Step three: When I inspect the sharing directly on the object from the salesforce site, it shows me that both reasons are now attached to the object
Is my problem inserting and not upserting?
if you want to delete Shares (or any other records) from the database, you have to do a delete DML statement. Removing them from the list and then inserting that list again will not touch them in the database. Or did you just not post the relevant lines of code?
I recommend you lop through your list, and add any Shares you want to delete to a second list (e.g. deleteList), then do a
delete deleteList;