Escape underscore (_) in SOQL LIKE clause

escapequerysoql

Before you point me to LIKE Clause documentation, I already read it.

I created new custom object thats API name is TestObject__c.

Now I would like to query my custom objects. So I need to find the ones that end with "__c".

LIKE '%\_\_c'

SELECT QualifiedApiName FROM EntityDefinition WHERE QualifiedApiName LIKE '%\_\_c' ORDER BY QualifiedApiName

Escaping underscore (_) with backslash (\) is not working as expected.

I tried a lot of variations but nothing works. I get 0 objects in result. When I do not escape underscores, I get 14 objects (1 custom, 13 standard), basically all ending with "c".

I tested this in Developer Console.

Best Answer

The better approach here is to filter based on the key prefix.

Custom objects always start with 'a', starting with 'a00'. There's a nifty Salesforce help article that lists all of the prefixes used.

SELECT QualifiedApiName FROM EntityDefinition WHERE KeyPrefix LIKE 'a%'

Related Topic