Salesforce Fact #838 | Database.querywithBinds() usage

With the new Database.querywithBinds() method, we can use bind variables as map of String, Object.

It also supports multiple values, and even works with list of Sobject.

Reference: https://salesforce.stackexchange.com/questions/397098/dynamically-pass-bind-variables-to-a-soql-query-with-querywithbinds-method

Below are the code samples:

//works with list of String

Map<String, object> bindVars = new Map<String, Object>();

bindVars.put('accNames', new List<String>{'Test Acc1', 'Test Acc2', 'Test Acc3'});

String query = 'SELECT Id, Name, Rating FROM Account WHERE Name IN :accNames';

List<Account> accList = Database.queryWithBinds(query, bindVars, AccessLevel.USER_MODE);


//works with list of Sobject as well

List<Account> accList1 = [SELECT Id FROM Account WHERE Id 

                         IN ('0016F00002RrZdsQAF', '0016F00002RrZdtQAF', '0016F00002RrZduQAF')];

Map<String, object> bindVars = new Map<String, Object>();

bindVars.put('Accounts', accList1);

String query = 'SELECT Id, Name FROM Contact WHERE AccountId IN :Accounts';

List<Contact> conList = Database.queryWithBinds(query, bindVars, AccessLevel.USER_MODE);

Note: The above code snippets are only for demonstration purpose




Comments

Popular posts from this blog

Salesforce Fact #192 | Call batch apex from flow

Salesforce Fact #457 | Get current user role name in flow