function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yassine ZouagYassine Zouag 

Give access to record data even though the user doesn't have access to it

Hello, 

In my test class, I want the user to be able to display contact data (name, phone, etc, which will be shown in a table), even for contacts to which he has no access. Of course, he wouldn't be able to access the page record (since he doesn't hava read permission.

However, when my query is run as the user, no contacts are shown.
Select Id, Name, Phone from Contact;

Do you know a workaround ?
Thanks.
 
James LoghryJames Loghry
Are you talking a unit test class? If so, no data is available by default in a unit test.  You'll have to create mock contact records and then run a SOQL query on your created records if need be.
Yassine ZouagYassine Zouag
Hello James, exactly.
In fact, in my test class, I create a contact as user1. Then, I run my query as user2 using the runAs() method. However, my query returns an empty list.
That's because user2 cannot read or edit contacts created by user1. What I want to do is to be possible for user2 to see all contact fields (e.g. name, phone) without having access to the page record.

 
James LoghryJames Loghry
Try adding "without sharing" to your test class's definition.  If that doesn't work, try creating a separate class with without sharing, and then add your Contact query as a helper or utility method in that class.