+ Start a Discussion
Ashok Reddy 61Ashok Reddy 61 

Getting query results in Workbench , Not in apex class/Script

Background : I need access account records in the apex class where name contains %

For example, the query should return below accounts
abcd10%
cdcd40%abcd
10%volume

When i execute below query i am getting results in the workbench
select id,name from Account where Name LIKE '%\%%'


the same query If I execute in Anonymous window/apex execute getting no results

 
Ashok Reddy 61Ashok Reddy 61

List<Account> accList = [select id,name from Account where Name LIKE '%\%%'];
system.debug('accList :::'+accList.size());

If we observe below image, it is appending extra / in the query and giving wrong results. 

Can you please help me how to resolve this?
User-added image
Linga_RaminLinga_Ramin
Hi Ashok,

You can try Query like this.It worked for me

SELECT Id,Name from Account where Name LIKE '%\%'
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ashok,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
String searchString = '%\\%%';
List<Account> accList = [select id,name from Account where Name LIKE : searchString];
System.debug('accList -> ' + accList);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ashok Reddy 61Ashok Reddy 61
Thanks for your time. 

Tried below snipet and worked :)
string s='\\%';
system.debug('!!!!='+s);
String query = 'SELECT Id,name FROM Account WHERE Name LIKE \'%'+S+'%\'';
List<Account> accList = Database.query(query);
system.debug('accList :::'+accList);