Created
April 5, 2024 12:31
-
-
Save aidan-harding/0e2c3d91318525e0bc33c28b2d7608de to your computer and use it in GitHub Desktop.
Check Salesforce object and field access by querying EntityDefinition and EntityParticle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note that WITH USER_MODE is not allowed in Anonymous Apex! | |
List<EntityDefinition> entityDefinitions = [ | |
SELECT QualifiedApiName | |
FROM EntityDefinition | |
WITH USER_MODE | |
]; | |
// Now entityDefinitions is a list of objects accessible by the current user, | |
// a bit like Schema.getGlobalDescribe().keySet() but faster! | |
List<EntityDefinition> entityDefinitionsAndFields = [ | |
SELECT | |
QualifiedApiName, | |
( | |
SELECT QualifiedApiName, IsUpdatable, IsCalculated, Length, DataType, Label | |
FROM Particles | |
WHERE IsCompound = FALSE | |
) | |
FROM EntityDefinition | |
WHERE QualifiedApiName IN :objectNames | |
WITH USER_MODE | |
]; | |
// Now entityDefinitionsAndFields is a list of objects and fields that the running user can access | |
// Don't call this with no filter in a big org or you might exceed the 50k query row limit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment