Query.apex
Query.apex copied to clipboard
Using enforceGlobalSecurity and lookup() throws System.QueryException: expecting an equals sign, found ')'
Consider example:
Query.enforceGlobalSecurity();
Query q = new Query(Account.SObjectType)
.selectFields(new String[]{'Name'})
.lookup('Id',
new Query(Contact.SObjectType)
.selectField(Contact.AccountId));
System.debug(q.toQueryString());
q.run();
Which creates query
SELECT name FROM Account WHERE (Id IN (SELECT accountid FROM Contact WITH SECURITY_ENFORCED)) WITH SECURITY_ENFORCED
The problem is with inner join query has WITH SECURITY_ENFORCED which fails SOQL parser.
As a workaround you can disable security check for lookup query:
Query q = new Query(Account.SObjectType)
.selectFields(new String[]{'Name'})
.lookup('Id',
new Query(Contact.SObjectType)
.enforceSecurity(false)
.selectField(Contact.AccountId));
Nice catch.
I think we need to get rid of the WITH SECURITY_ENFORCED phrase, if there is one, inside the lookup method.