4

I have been going through the forums and the documentation and I do not find a way to query the Users who have access to a particular record.

For example, when I upload a document, a mail has to be sent to those Users who have access to that particular document. For this, I need the UserId.

The UserRecordAccess object doesn't allow us to query the UserIds. How can we do this?

4

You can use userRecordAccess object to check CRUD permissions..

So pass the recordId and userId.. to check that user having what type of access on that record..

you can check variables like HasDeleteAccess. If HasDeleteAccess true means user have delete access. If false then user don't have access..

Based on these accessibility perform DML..

List<UserRecordAccess> lstUserRecordAccess = [SELECT RecordId,MaxAccessLevel,HasAllAccess, 
                                                     HasDeleteAccess, 
                                                     HasEditAccess,
                                                     HasReadAccess, 
                                                     HasTransferAccess 
                                                FROM UserRecordAccess 
                                               WHERE UserId = '00550000000rlrX' 
                                                 AND RecordId = '0015000000TaWdI'];

| improve this answer | |
  • 2
    nice answer. +1 – Tushar Sharma Feb 2 '16 at 5:35
  • 1
    Yes, but this is not my requirement. I have a record and I need the list of users who have access to it. How do I do this? In userRecordAccess, I can only see if the CRUD permissions are there for a given user but I cannot have the UserId in the query result – User2529 Feb 2 '16 at 5:36
  • @User2529 then don't give userId in where condition instead query this field. – Tushar Sharma Feb 2 '16 at 5:36
  • 1
    @User2529 you have to specify useId in WHERE condition else you will get this error Where clause must contain UserId = [single ID] – Ratan Paul Feb 2 '16 at 5:40
  • 1
    @ratan check it out – User2529 Feb 5 '16 at 9:33
1

I found a work-around but it isn't the best of practices.

I put all the users who had access to a particular library in a public group. Then when I inserted a document into a library, I check the library by using its Id. Once I have the library, I query all the users of that particular group and take their email ids and mail a notification to them.

This isn't the best practice as I had to hard-code the ParentId in the Content Document to check the library the content was inserted in.

If someone can come up with a better solution, I'd be grateful!

| improve this answer | |

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.