I am looking to see if it is possible to compare field level security between profiles using a SOQL statement. Is this possible please?
3 Answers
Here is how you can get the Field Level Security for particular profile:
SELECT Id, Field, SObjectType, PermissionsRead, PermissionsEdit
FROM FieldPermissions
WHERE parentId IN ( SELECT id
FROM permissionset
WHERE PermissionSet.Profile.Name = 'System Administrator')
Depending upon the objects in your org, the above query would return number of records i.e. each record represents a field on each object. So for 20 objects with each of them having 20-50 custom fields it would return 20*50 number of records.
You can restrict the number of records by looking at a specific sObject. Ex:
SELECT Id, Field, SObjectType, PermissionsRead, PermissionsEdit
FROM fieldPermissions
WHERE SObjectType = 'Opportunity'
AND parentId in ( SELECT id
FROM permissionSet
WHERE PermissionSet.Profile.Name = 'System Administrator')
Hope this helps.
I believe you can get basic info like filterable, updateable etc using the field portion of the DescribeSObjectResult. But, it is not complete and is only for the current user. If you really need to get this info using non-metadata methods, you might have to go to Andrew Fawcett's excellent metadata wrapper classes.
Good luck!
// Find if Profile has Read, Edit access to specific field
SELECT Id, Parent.Profile.Name, Field, SObjectType, PermissionsRead, PermissionsEdit
FROM FieldPermissions
WHERE parentId IN ( SELECT Id
FROM permissionset
WHERE PermissionSet.Profile.Name IN (YOUR PROFILES))
AND SObjectType = YOUR SOBJECT AND Field = YOUR FIELD
-
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.– Community BotCommented Oct 26, 2023 at 13:29