0
votes

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 the 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.

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

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

Can you please help me how to resolve this?

16:00:21:002 SOQL_EXECUTE_BEGIN [1]|Aggregations:0|SELECT id, name FROM Account WHERE Name LIKE '%\%%'

1

1 Answers

0
votes

You could use a bind variable or dynamic SOQL.

String searchText = '%\\%%';

List<Account> accs = [SELECT Id, Name 
    FROM Account 
    WHERE Name LIKE :searchText];
System.debug(accs.size());
System.debug(accs);

// Or in dynamic query (looks bit crazier because ' has to be escaped too)

List<Account> accs2 = Database.query('SELECT Id, Name FROM Account WHERE Name LIKE \'%\\%%\'');
System.debug(accs2);

debug output with 3 rows similar to OP's sample data