function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SVsfdcSVsfdc 

SOQL Query for Checklist type using LIKE operator

Please help me on SOQL query using Like operator for Checklist Type in SF.
SELECT Order_Type__c,Part_number__c FROM Product where Order_Type__c 'Purchase%'

The below values are for Order_type__c field.
Annual Lease;Purchase;Short Term Lease
Fee
Lease
Lease;Purchase
Lease;Purchase;Short Term Lease
 
Best Answer chosen by SVsfdc
PawanKumarPawanKumar
Just small correction.

Please use below.

String str='Lease;Purchase;Short Term Lease';
String queryStr = 'SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c includes(\''+str+'\')';
System.debug(queryStr);
List<Product2> plist= Database.query(queryStr);
System.debug(plist);
 

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
String str='Purchase%';
List<Product2> plist=[SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c LIKE :str];
SVsfdcSVsfdc
Thank you Bala for response.
What query you suggest If I'm using Developer Console?
I'm getting the below error 'Unknown error parsing query'
Balayesu ChilakalapudiBalayesu Chilakalapudi
Write a debug statement like this,
System.debug('plist size:'+plist.size());

Click on DEBUG > Open Execute anonymous window > paster the above two statements > click on execute > you can find the results in log's tab
HARSHIL U PARIKHHARSHIL U PARIKH
Try someting like below,
 
String str='Purchase';
List<Product2> plist=[SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c LIKE :('%' + str + '%')];

Hope it helps!
SVsfdcSVsfdc
I'm getting the below error message:

Part_number__c FROM Product2 where Order_Type__c LIKE :('%' + str + ^ ERROR at Row:1:Column:57 invalid operator on multipicklist field
PawanKumarPawanKumar
Please use includes operator like below.

String str='Lease;Purchase;Short Term Lease';
List<Product2> plist=[SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c includes :str];
 
PawanKumarPawanKumar
you can refer below link as well.
https://help.salesforce.com/articleView?id=000211744&type=1
SVsfdcSVsfdc
Thank you for the response. Looks like something wrong with str.
PawanKumarPawanKumar
what error you are getting now in my posted query?

Please try below 
String str='Lease;Purchase;Short Term Lease';
List<Product2> plist=[SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c includes('Lease;Purchase;Short Term Lease')];
SVsfdcSVsfdc
error was unexpected token: '<'

Thank you Pawan this new query worked.
 
PawanKumarPawanKumar
Good to know static query is working fine.

You are getting that error because Includes operator does not supports bind varaibles(:VarName) concepts.

So please try below and let me know the output. Here i am using dynaic SQL in order to make str dynamic.

String str='Lease;Purchase;Short Term Lease';
String queryStr = 'SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c includes('+str+')';
List<Product2> plist= Database.query(queryStr);
System.debug(plist);
PawanKumarPawanKumar
Just small correction.

Please use below.

String str='Lease;Purchase;Short Term Lease';
String queryStr = 'SELECT Order_Type__c,Part_number__c FROM Product2 where Order_Type__c includes(\''+str+'\')';
System.debug(queryStr);
List<Product2> plist= Database.query(queryStr);
System.debug(plist);
 
This was selected as the best answer
SVsfdcSVsfdc
That worked Pawan.

Thank you somuch Pawan and also Bala and Govind.
I'm new to Sf and I learned how to handle Type Checklist.
PawanKumarPawanKumar
Great to know. Please give me a favour by marking best answer.