Dynamic SOQL refers to the creation of a SOQL string at run time with Apex code. Dynamic SOQL enables you to create more flexible applications. For example, you can create a search based on input from an end user or update records with varying field names.
In general we write advance / dynamic query something like this
sObject objectRecord = Database.query('SELECT Id , Name FROM Account LIMIT 1');
if it is for collection then something like this
List<sObject> objectRecord = Database.query('SELECT Id , Name FROM Account');
But in Spring'23 we got new update from salesforce i.e query Binds, with the help of query binds we can pass the dynamic query variable reference without any query scope limits
With Database.query, All variable references need to maintain within the Apex Class Query scope. By using Database.queryBind no need to worry about scope of query and variables all this handled by BindsMap.
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);
Comentários