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
lapaullapaul 

Does SOQL support wild card?

Does SOQL support wild card?


Select Name, Email, Phone From Lead where Name like %searchText%

 

does it work for the SOQL command above? please advice.

 

thanks

SuperfellSuperfell

Yes, Select Name, Email, Phone From Lead where Name like '%searchText%'

 

See the SOQL section of the API docs. 

lapaullapaul

thanks for your reply.

 

results = (List<Lead>) [Select Name, Email, Phone From Lead where Name like '%searchText%'];

 

I just tried the SOQL command above in my Apex code but it does not seem to work. Is it correct syntax?

I entered Tom in the search input box and it returned nothing. I know for sure there's a name Tom James in

the Lead. Any ideas?

 

thanks

Paul

SuperfellSuperfell
If that's your exact code, its looking for anything with the literal searchText in it. you need to feed your input in to it.
lapaullapaul

yes that is my exact code. I'm not sure about the wild card SOQL syntax. If you do please help

with example.

 

thanks

 

SuperfellSuperfell

// you have to set the searchText variable to whatever it is you're tying to find from whereever its coming from.

 

String fnd = '%' + searchText + '%';

List<Lead> = [select id, name from Lead where name like :fnd];

 

lapaullapaul

Thanks so much. it works.

You saved me hours of research.

 

Have a great day!

 

Paul