Dynamically Pass Bind Variables to SOQL Query

Telegram logo Join our Telegram Channel

Hello Trailblazers! Using dynamic SOQL is easier than it was before, previously we could use only the bind variables from the current block of code where we are calling the Database.Query function.

With the Spring 2023 release, you can use the new function which allows you to pass the bind variables dynamically in the form of a map. So we can pass it from other functions/classes. Database.queryWithBinds which accepts three arguments.


The Database.queryWithBinds method

  • Query String (String)

    This is the query that you want to run.
    For example: Select Id from Contact Where Name: name

  • Bind Parameters (Map<String, Object>)

    This is the map of the bind variables where the key is the name of the bind variables.
    For example name from the above query. Value is the bind data that you want to pass to the query, for example. a name of the contact i.e. Rahul Gawale.

    Please note that the bind variables are case-sensitive here.

    You can pass primitive data as well as lists or arrays or sets of primitive data types. For example List<Id>.

  • Access Level which is Enum from AccessLevel class (AccessLevel.USER_MODE, AccessLevel.SYSTEM_MODE)

    This option allows us to select user or system mode to run the SOQL query.


Examples:

Example with simple primitive data types:

String query = 'SELECT Id,Name FROM Contact WHERE Name = :name';
Map<String, Object> bindParams = new Map<String, Object>();
bindParams.put('name', 'Rahul Gawale');
List<Contact> contacts = (List<Contact>)Database.queryWithBinds(query, bindParams, AccessLevel.USER_MODE);
System.debug('contacts ' + contacts);

// Find accounts with more than 10 employees
String query = 'SELECT Id,Name FROM Account WHERE NumberOfEmployees >= :employeeCount';
Map<String, Object> bindParams = new Map<String, Object>();
bindParams.put('employeeCount', 10);
List<Account> accounts = (List<Account>)Database.queryWithBinds(query, bindParams, AccessLevel.USER_MODE);
System.debug('accounts ' + accounts);

Example with List/Set:

// Find contacts with given contact ids
List<Id> accountIds = new List<Id>{ '0012800000IOph8AAD', '0012800000IOph7AAD' };
String query = 'SELECT Id, Name From Contact WHERE AccountId IN :accountIds';

// put the data into map
Map<String, Object> bindParams = new Map<String, Object>();
bindParams.put('accountIds', accountIds);

// run the query
List<Contact> contacts = (List<Contact>)Database.queryWithBinds(query, bindParams, AccessLevel.USER_MODE);
System.debug('contacts ' + contacts);

Example with multiple bind parameters:

// Get accounts with the name starting wit ABC Corp and Employee count greater than equals to 10
String query = 'SELECT Id,Name FROM Account WHERE Name Like :name AND NumberOfEmployees >= :employeeCount';
Map<String, Object> bindParams = new Map<String, Object>();
bindParams.put('name', 'ABC Corp%');
bindParams.put('employeeCount', 10);
List<Account> accounts = (List<Account>)Database.queryWithBinds(query, bindParams, AccessLevel.USER_MODE);
System.debug('accounts ' + accounts);

No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com