Retrieve The list Of Profiles That Have Access To A particular Object – SOQL Interview Inquiry

This SOQL interview question will familiarize you with the process of using SOQL to obtain the names of all profiles that have access to a specific object.

Recently, a particular interview question has been frequently posed in Salesforce interviews, particularly in the context of SOQL.

The question is as follows: How can one obtain the list of profiles that have specific permissions on a particular object?

The corresponding query is outlined below!

List<PermissionSet> permissions = [SELECT Profile.Name FROM PermissionSet WHERE IsOwnedByProfile = TRUE AND Id IN (SELECT ParentId FROM ObjectPermissions WHERE PermissionsCreate = True AND PermissionsRead = True AND PermissionsEdit = True AND PermissionsDelete = True AND PermissionsRead = True AND SObjectType = 'Account') ORDER BY Profile.Name];
System.debug(' 🚀 ' +permissions);


If you’re keen on comprehending how this operates, keep on reading.

Each profile will be linked to a Permission Set. Here’s a visual confirmation of that.

PermissionSet permission = [SELECT Id FROM PermissionSet WHERE PermissionSet.Profile.Name = 'System Administrator'];
Query

Moving on to the next segment, information about objects and their corresponding permissions is accessible through the ObjectPermissions object.

The ObjectPermissions object includes a foreign key named ParentId, which refers to the primary key in the PermissionSet object.

Hence, the subsequent query will furnish a list of PermissionSet Ids along with their associated permissions.

List<ObjectPermissions> objectPermissions = [SELECT ParentId FROM ObjectPermissions WHERE PermissionsCreate = True AND PermissionsRead = True AND PermissionsEdit = True AND PermissionsDelete = True AND PermissionsRead = True AND SObjectType = 'Account'];

Now, I will leverage this list to retrieve the names of all profiles from the PermissionSet object, utilizing the Id field (primary key) in the WHERE clause. I understand that it might seem overwhelming at first glance.