Recently I asked a question and this is a follow up to that one

So I got it all worked out, but now my question is how to do I delete the record that I added? When users create this form it goes for approval, and during that approval someone may have to change the Rep, because they may make a mistake or simply don't know who it is on creation, so if someone changes the Rep I want to delete their sharing privileges that I initially gave them. Now I'm being told it can't be done, and they deal with it already in other instances(manual process). But I think it can be done. The following is my code, at the top it is properly adding the user, but I also now check to see if the oldMap != the new one. I've been looking into DeleteResult[] and delete, but I don't see many examples and I'm pretty confused on how to move forward.

        splitSharesToInsert.add(new Split__Share(ParentId = s.id , 
            UserOrGroupId = Rep3SetMap.get(s.Rep_3__c).Commission_Correction_User__c, 
            AccessLevel = 'Read'));

            Database.Saveresult[] rsr3 = Database.insert(splitSharesToInsert, false);                             
    if(Trigger.oldMap.get(s.Id).Rep_3__c != s.Rep_3__c){ 
        // delete rsr3;          
        // Database.DeleteResult[] drsr = Database.delete(splitSharesToInsert,false);
}catch (NullPointerException e){ 
    System.debug('NullPointerException' + e);   
  • This is a trigger, right? Why do you need to add and delete the same record within a trigger? Just don't add the record if it matches your delete criteria. Salesforce recommends keeping triggers efficient, so have just one Database operation (or delete) is better.
    – Jagular
    Commented Jun 13, 2014 at 0:05
  • I add the Sharing rights, the issue is if someone changes the Rep 1, the first person(incorrect person) will still have the sharing
    – EricSSH
    Commented Jun 13, 2014 at 15:55

1 Answer 1


Simply run a query for the permission query and then run a delete on it:

delete [SELECT Id FROM Split__Share WHERE ParentId = :pId AND UserOrGrouppId IN :myGroup];

2 things to note

1) You should delete the shares BEFORE adding the new ones so that you don't essentially have 2 copies of the same thing. This will cause an error in the database.

2) You likely need to turn the above query into a dynamic query because you need to build the ParentIds and UserOrGroupIds in pairs rather than a whole set together. If this is something you need help on, then a second related question would be likely a good idea so that if you run into issues you aren't confusing things.

You must log in to answer this question.

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