I've tried a number of ways including:

SELECT Id, Name FROM Account WHERE Id LIKE '0013%'


FIND { 0013 } IN ALL FIELDS RETURNING Account (Id, Name)

and all I receive is the same


Is this to say that I am unable to search based on partials of the Id field? I can only retrieve based a whole Id field?

Edit: The problem I am trying to solve is the use of the Id field as an integration value with another system.

e.g. We use the ID from salesforce in a seperate system to tie the records together. We have built a small tool where I would like to provide an auto-complete when typing in one of these IDs, directly from salesforce.

  • 4
    I am curious about the use case for this. Why do you need to do that? – PepeFloyd Feb 11 '13 at 23:11
  • 1
    What part of the ID has any meaning to search on? The key prefix will always be 001 for Accounts. Are you looking for Accounts that were created in a certain pod? – Daniel Ballinger Feb 11 '13 at 23:12
  • I'll throw in a "me too" on this. Looking for patterns in the Id field has a strong code smell to me. Maybe Darien can post the problem he's trying to solve by searching on the Id field using a pattern, and we can come up with better solutions. – tomlogic Feb 13 '13 at 19:11

You can not use the LIKE operator with an Id field. As per 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 _.
  • Do not use the backslash character in a search except to escape a character.

For example, the following query matches Appleton, Apple, and Bappl , but not Appl:

SELECT AccountId, FirstName, lastname
FROM Contact
WHERE lastname LIKE 'appl_%'

Please note the bolded section. In Salesforce, Id fields are actually considered a primitive type of Id. Please check out the Salesforce documentation, specifically the ID Field Type section. Essentially, there is no way to do that directly.

To be honest with you, I would be interested to even hear a use case for this type of situation. If you absolutely must do this, here is a workaround. Create a text field and use a workflow field to update it on create of the object. The field should be readonly. You can then search on this field. The reason you make it a text field rather than a formula is because the field can then be indexed. This has more to do with performance though. Good luck with whatever you are attempting!

  • I am looking on how to perform a like on an ID field. In my case, I am creating a report through Apex for Pending Approvals (since I need to schedule a report to email a group of their pending approvals every hour during office hours). I am trying to filter the targetObjectId on the ProcessInstance to pull items for the object that I am creating my report for. However, being unable to apply a like condition to the ID is causing an issue for me. I will need to query all of them, and then loop through them to add the ones I need in a separate list for processing. – smckitrick Aug 26 '20 at 15:02

According to the SOQL Documentation on Comparison Operators the Like is just for Strings.

Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value.

You could create a Text formula field and give it the value of the Id field by specifying Id in the formula editor. Then your query would be as follows (assuming field named Id String).

Select Id, Name From Account Where Id_String__c Like '0013%'

If you are worried about performance of the query using a formula field, as of Winter '13 you can contact Salesforce Customer Support to request indexes on deterministic formula fields, which I would think Id qualifies. See this related blog post.

  • 4
    Peter! You beat me by a minute! Either way, upvote. Good find! However, for performance sake, using a LIKE search operator with a formula is inefficient. Check out the Customer Index Considerations for Selection SOQL Queries - salesforce.com/us/developer/docs/apexcode/Content/… – Jesse Altman Feb 11 '13 at 23:15
  • @JesseAltman - Thanks! Good to know! I imagine this would be particularly poorly performing on potentially larger tables like Account. – Peter Knolle Feb 11 '13 at 23:19
  • 1
    This is the approach I've always used, with regards to inefficiency I expect the user's connection is far more likely to be the bottleneck in a given operation. – Matt Lacey Feb 11 '13 at 23:41

Yes, you can do this, but you'll have to do it on a mirror of the ID rather than the ID itself, since LIKE is only supported on string fields. Just create a formula field that returns the ID and filter on that.

select Id from Object__c where Id_Mirror__c Like '0013%'

Like isn't very efficient and formula fields make it less so. You can adapt this approach by creating a using a text external id field for the mirror and then populating it by workflow on create.

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.