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
VINODKUMAR REDDY KALUVAVINODKUMAR REDDY KALUVA 

Like Operator is not working with Dynamic Variable in SOQL Query

Hi,

Here I'm trying to perform LIKE operation in SOQL when I'm passing dynamically its not returning results?
List<string> companies=new list<string>();
List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account WHERE Name LIKE : '%' + companies + '%'];
I tried Like below ways
LIKE  '%+ companies +%'
LIKE: '%companies %'
LIKE:('%'+companies+'%')

Any One Please help me?

Thanks In Advance!..
SandhyaSandhya (Salesforce Developers) 

Hi,
 

Try below code

String str='%'+companies+'%';

 List<Account> leadAccountIds=  [SELECT Id , OwnerId, Name FROM Account WHERE Name LIKE : str];

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
 
VINODKUMAR REDDY KALUVAVINODKUMAR REDDY KALUVA
I tried in that, but the query is not returning any result.
pankul guptapankul gupta
Don't give colons in between, try the below, it should work:
 
List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account WHERE Name LIKE  '%companies%'];
Let me know if the same resolves your query.
 
VINODKUMAR REDDY KALUVAVINODKUMAR REDDY KALUVA
@pankul  No, I tried that as well
PawanKumarPawanKumar
Please try below.

List<string> companies=new list<string>();
companies.add('%test%');
companies.add('%pawan%');
    
List<Account> accountList = [Select Id, OwnerId, Name FROM Account WHERE Name LIKE : companies];
System.debug(accountList);

Please mark it best if it helps you. Thanks.