“like operator only valid on string field” Exception in Salesforce

“like operator only valid on string field” Exception in Salesforce

“like operator only valid on string field” exception occurs if the LIKE operator in SOQL is used again non-string fields.
 
In SOQL, the LIKE operator is supported for string fields only. So, we cannot use it against any other field data types.

 
Example:


String sql = ‘Select Name From Account WHERE Name LIKE ‘%’ + searchKeyword + ‘%”;

where searchKeyword is an Apex variable.


(or)


List<Contact> listCon = [SELECT Id, FirstName FROM Contact WHERE FirstName LIKE ‘%test%’];
system.debug(‘List contacts are ‘ + listCon);
 
 

Leave a Reply