5

I want to check whether context user is having "view all" permission on an object or not. Is there any way to know that?

6

Sure!

Check the ObjectPermissions table and here's the Entity Rel. Diagram.

SELECT SobjectType, PermissionsViewAllRecords, PermissionsModifyAllRecords
FROM ObjectPermissions
WHERE Parent.Profile.Name = 'System Administrator'
ORDER BY SobjectType 
LIMIT 10

In Apex it could even be WHERE SobjectType = 'Account' AND Parent.ProfileId = :UserInfo.getProfileId().

| improve this answer | |
2

@Eyescream is on the right track, but you'll also want to take into account permission sets which can also grant the view all perm. Here's an example function that checks both

public Boolean hasViewAllPerm(Id userId, Id profileId, String sObjectType) {
    Integer permSetCheck = [
        select count()
        from ObjectPermissions
        where sObjectType = :sObjectType
            and PermissionsViewAllRecords = true
            and parentId in (
                select permissionSetId 
                from PermissionSetAssignment 
                where assigneeId = :userId
            )
    ];
    Integer profileCheck = [
        select count()
        from ObjectPermissions
        where sObjectType = :sObjectType
            and PermissionsViewAllRecords = true
            and parent.profileId = :profileId
    ];
    return (permSetCheck + profileCheck) > 0;
}
| 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.