List<Contact> contactsWithSecrets = [
SELECT
Name, Social_Security_Number__c
FROM
Contact
WITH SECURITY_ENFORCED
];
If fields or objects referenced in the SELECT clause using WITH SECURITY_ENFORCED are inaccessible to the user, the query throws an exception indicating insufficient permissions and no data is returned.
There is a new restriction while querying Polymorphic lookup fields using WITH SECURITY_ENFORCED. Polymorphic lookup fields are relationship fields that can point to more than one entity.
1. Traversing a polymorphic field’s relationship is not supported in queries using WITH SECURITY_ENFORCED.
2. Using TYPEOF expressions with an ELSE clause is not supported in queries using WITH SECURITY_ENFORCED.
3. The Owner, CreatedBy, and LastModifiedBy polymorphic lookup fields are exempt from this restriction, and do allow polymorphic relationship traversal.
It is recommended, using WITH SECURITY_ENFORCED in Apex classes or triggers with an API version 45.0 or later.
One thought on “Enable Field- and Object-Level Permissions Checking Using WITH SECURITY_ENFORCED in SOQL Queries”