8

I'm using apex managed sharing to grant permissions very similar to how it is done in this example trigger: http://wiki.developerforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic

However, I also need the ability to revoke or delete this access and having it revert to private. The reason for this is my understanding is that there are only two access options for managed sharing access 'read' or 'edit' with no 3rd option of 'none' or 'private'.

Reference: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_bulk_sharing_creating_with_apex.htm

I've tried adding the shares to a list and deleting the list and I'm getting the error: DELETE_FAILED, cannot delete owner or rule share rows,

I also noticed there is a field on the share object 'isDeleted' this field however is not writable.

Does anyone have experience revoking or deleting these permissions once they've been created?

Thanks!

1 Answer 1

13

You will need to delete a specific type of sharing record, those with a RowCause value of 'Manual'.

Delete all manual sharing records on a custom object contained in the trigger:

List<MyCustomObject__Share> sharesToDelete = [SELECT Id 
                                                FROM MyCustomObject__Share 
                                                WHERE ParentId IN :trigger.newMap.keyset() 
                                                AND RowCause = 'Manual'];
if(!sharesToDelete.isEmpty()){
    Database.Delete(sharesToDelete, false);
}

Similarly for a standard object (Account):

List<AccountShare> sharesToDelete = [SELECT Id 
                                        FROM AccountShare 
                                        WHERE AccountId IN :trigger.newMap.keyset() 
                                        AND RowCause = 'Manual'];
if(!sharesToDelete.isEmpty()){
    Database.Delete(sharesToDelete, false);
}
6
  • 1
    Can you please elaborate on this answer more. I assume sObjectShareName is a String like Custom_Object__Share, but what kind of where clause are you using? Also it is VERY bad practice not to escape your variables using String.escapeSingleQuotes (or maybe you are, but that's why I'm asking for elaboration) Feb 28, 2013 at 22:27
  • The previous code example came from a utility class that was called by many triggers to perform manual sharing recalculation, which is why it was genericized that way. Updated to more concrete samples.
    – Mark Pond
    Feb 28, 2013 at 23:08
  • Can anyone Please suggest, what to do, when I want to delete the List<MyCustomObject__Share> sharesToDelete = [SELECT Id FROM MyCustomObject__Share WHERE ParentId IN :trigger.newMap.keyset() AND RowCause = 'Owner']; May 17, 2017 at 13:35
  • @AaryaTiwary We would be happy to answer that question if you would ask it someplace other than a comment. Click 'Ask Question' at the top of the page and fill in your query.
    – Mark Pond
    May 19, 2017 at 4:03
  • @MarkPond can we revoke access by using apex class?? and i am trying to use dynamic values.
    – user48859
    Oct 9, 2017 at 15:23

You must log in to answer this question.

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