Query.apex icon indicating copy to clipboard operation
Query.apex copied to clipboard

Using enforceGlobalSecurity and lookup() throws System.QueryException: expecting an equals sign, found ')'

Open sjurgis opened this issue 3 years ago • 1 comments

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));
                       

sjurgis avatar Jul 06 '22 20:07 sjurgis

Nice catch.

I think we need to get rid of the WITH SECURITY_ENFORCED phrase, if there is one, inside the lookup method.

HenryRLee avatar Jul 07 '22 00:07 HenryRLee