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
subaasubaa 

How to use LIKE clause in the query for dynamic search text?

Hi,

 

We got a requirement to pull the records which contains '123' in a particular column. So, we used LIKE '123%' clause in the query. However, the search text value will be entered dynamically and hence we tried to change the query with class varibale for the search text. But it fails for the below where clause.

 

LIKE "'"+searchText+"%'"

 

Can anyone help me to fix this issue.

 

Regards,

SuBaa

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

Please check below options

 

string inVar='a';
string tempInput = '%' + inVar + '%';
List<Contact> lst = [Select Id , FirstName, LastName from Contact where lastname like  : tempInput ];
System.debug('------------'+lst);

 

 

Thanks ,

Bala

All Answers

bob_buzzardbob_buzzard

I remember having problems trying to build up the string in this way.  The way I found that worked was to build the wildcarded string in a variable as follows:

 

String val='\'%' + String.escapeSingleQuotes(inVal.trim()) + '%\'';

 

and then use this in the query:

 

 ' LIKE ' + val;

 

 

b-Forceb-Force

Please check below options

 

string inVar='a';
string tempInput = '%' + inVar + '%';
List<Contact> lst = [Select Id , FirstName, LastName from Contact where lastname like  : tempInput ];
System.debug('------------'+lst);

 

 

Thanks ,

Bala

This was selected as the best answer
aklkkaklkk
hey bob_buzzard

I have a 1 qustion can you slove because having a problem ?

how to perform upsert operation with the Trigger because want to perform that insert the record if record all ready exist then update else create a new Record ??

Please Slove