0

I need to use SOQL LIKE operator with IDs?

List<Account> accounts = [SELECT Id FROM Account WHERE Id LIKE '001%'];

I'm aware that the LIKE operator is supported for string fields only, however is there any possible workaround to somehow use SOQL LIKE operator with IDs?

Please advise if there is any possibility to use LIKE operator with IDs?

  • 6
    Why do you want to do this? IDs are independent of each other except that they indicate the sObject type they point to. Are you trying to determine the type of a lookup in a dynamic ID field? – IllusiveBrian Oct 20 '19 at 20:32
3

Sorry to bring this up... but it is a no go with LIKE. You probably need to get some other logic:

From the documentation:

Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.

The % and _ wildcards are supported for the LIKE operator.

The % wildcard matches zero or more characters.

The _ wildcard matches exactly one character.

The text string in the specified value must be enclosed in single quotes.

The LIKE operator is supported for string fields only.

The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL. The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.

Don’t use the backslash character in a search except to escape a special character.

By the way, prefix '001' will retrieve all Accounts anyway...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.