0

I am sharing records of a custom object using User Managed Sharing through an Apex class

   CustomObj__Share teamShare  = new CustomObj__Share();

  // Set the ID of record being shared.
  teamShare.ParentId = customObj.Id;

  // Set the ID of user or group being granted access.
  teamShare.UserOrGroupId = user.Id;

  // Set the access level.
  teamShare.AccessLevel = 'Read';

  // Set rowCause to 'manual' for manual sharing.
  // This line can be omitted as 'manual' is the default value for sharing objects.
  teamShare.RowCause = Schema.CustomObj__Share.RowCause.Manual;

  // Insert the sharing record and capture the save result. 
  // The false parameter allows for partial processing if multiple records passed 
  // into the operation.
  Database.SaveResult sr = Database.insert(teamShare,false);

Everything works fine till here. When I log in as the user, I can see the custom object record but read only.

Now I need to do the same thing, but to revoke this given read (or edit) access to that user. In the documentation it says there is an access level named None, but it is only used for AccountShare.

How do I revoke access to the custom object using Apex? This apex code is called by a vf page where a user can grant/revoke access to the objects records using checkbox (or dropdown). So I am looking to solve revoke access using this vf page or a trigger.

Appreciate the help.

1

I think you can do something like this as described by Mark Pond in this link.

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);
}
  • It gives me an error saying "DELETE_FAILED, cannot delete owner or rule share rows". – codeinprogress Sep 22 '15 at 14:23
  • Nevermind, I got it. I was missing the rowcause condition. – codeinprogress Sep 22 '15 at 16:02
  • @Saroj Bera can we revoke access by using apex class?? and i am trying to use dynamic values – user48859 Oct 9 '17 at 15:23
  • @SFDCDeveloper What do you mean by dynamic values? – Saroj Bera Oct 9 '17 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.