Hello guys, today we are going to learn, how we can use INCLUDES operator in the dynamic soql. Here we will understand the whole concept using code example, so let's get started,

Use includes operator in dynamic soql 

Suppose we have a scenario in which we need to write a SOQL query to compare a list of string with the sobject's multiselect picklist field. so, while using SOQL INCLUDES operator with collection, we will need to convert list values to string format as it doesn't support bind expressions. Let's understand this concept with code sample.

Code Example:

 List<String> strlist=new List<String>{'Value1','test2'};
 String params='(\''+String.join(strlist,'\',\'')+'\')';
 System.debug('Params***'+params);
 String sQuery='Select id,Name FROM Contact WHERE MSP__c includes '+params;
 System.debug('Query***'+sQuery);
 List<Contact> conlist=Database.query(sQuery); 
 System.debug('Contactlist***'+conlist);
Output:


With the help of above code example, you can easily compare list values in the SOQL query.

Tip : You can follow the same code pattern for the "EXCLUDES" operator as well.

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.