Dynamically Pass Bind Variables to a SOQL Query - salesforce release'23
With the new Database.queryWithBinds , Database.getQueryLocatorWithBinds , and Database.countQueryWithBinds methods, the bind variables in the query are resolved from a Map parameter directly with a key rather than from Apex code variables. As a result, it’s not necessary for the variables to be in scope when the query is executed. Where: This change applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions. How: In this example, the SOQL query uses a bind variable for an Account name. Its value ( Acme Corporation ) is passed in using the acctBinds Map. Map<String, Object> acctBinds = new Map<String, Object>{'acctName' => 'Acme Corporation'}; List<Account> accts = Database.queryWithBinds('SELECT Id FROM Account WHERE Name = :acctName', acctBinds, AccessLevel.USER_MODE); SEE ALSO