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
sharrissharris 

Passing a variable into a LIKE statement in a SOQL query

Can anyone tell me how I can pass a variable into a LIKE statement in a SOQL query?

 

If I hardcode the search sting I get records returned:

results = (List<Solution>)[SELECT Id, SolutionNumber, SolutionName, TimesUsed FROM Solution WHERE (SolutionName LIKE '%keywordhere%')];

 

What I need to do is pass a variable in for the keyword. I tried the following code but I get an error - Error: SolutionSearch Compile Error: unexpected token: '+'

results = (List<Solution>)[SELECT Id, SolutionNumber, SolutionName, TimesUsed FROM Solution WHERE (SolutionName LIKE '%' + searchText + '%')];

 

I have this code that works but I don't like how it imprecise the returned record are that are returned:

results = (List<Solution>)[FIND :searchText IN ALL FIELDS RETURNING Solution(Id, SolutionNumber, SolutionName, TimesUsed)] [0];

 

Thank you.

 

Best Answer chosen by Admin (Salesforce Developers) 
RjSanchezRjSanchez

Okay, sorry about before. I set this up on my side to make sure this approach works.

 

String newSearchText = '%'+searchText+'%';

results = new List<Solution>([SELECT Id, SolutionNumber, SolutionName, TimesUsed FROM Solution WHERE SolutionName LIKE :newSearchText]);

 

 

All Answers

RjSanchezRjSanchez

Try this.

SELECT Id, SolutionNumber, SolutionName, TimedUsed FROM Solution WHERE SolutionName Like '%'+:searchText+'%'

Or with the parentheses:

 

results = (List<Solution>)[SELECT Id, SolutionNumber, SolutionName, TimesUsed FROM Solution WHERE SolutionName LIKE ('%' + searchText + '%')];

 

sharrissharris

Thanks for the reply!

 

When I used this:

results = (List<Solution>)[SELECT Id, SolutionNumber, SolutionName, TimedUsed FROM Solution WHERE SolutionName Like '%'+:searchText+'%'];

 

I got this:

Error: SolutionSearch Compile Error: unexpected token: '+'

 

When I used This:

results = (List<Solution>)[SELECT Id, SolutionNumber, SolutionName, TimesUsed FROM Solution WHERE SolutionName LIKE ('%' + searchText + '%')];

 

I got this:

Error: SolutionSearch Compile Error: unexpected token: '(' 

 

Any other thoughts?

Thank you.

 

RjSanchezRjSanchez

Here is something I am currently using. You can probably just modify it to suit your needs.

 

List<Handoff_Mapping__c> handoffUsers = new List<Handoff_Mapping__c>([SELECT User__c FROM Handoff_Mapping__c WHERE Apex_Mapping_Name__c LIKE :mappingName+'%']);

 hope it helps.

RjSanchezRjSanchez

Okay, sorry about before. I set this up on my side to make sure this approach works.

 

String newSearchText = '%'+searchText+'%';

results = new List<Solution>([SELECT Id, SolutionNumber, SolutionName, TimesUsed FROM Solution WHERE SolutionName LIKE :newSearchText]);

 

 

This was selected as the best answer
VempallyVempally
Hai,
What about the order of the string...

If I type "HP Laptop"...it working good but If I type "Laptop HP" its not working....

need some help...