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.
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);
Comments
Post a Comment