Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I've tried a number of ways including:

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

and

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

and all I receive is the same

INVALID_QUERY_FILTER_OPERATOR error.

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.

share|improve this question
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
add comment

3 Answers

up vote 7 down vote accepted

You can not use the LIKE operator with an Id field. As per the documentation:


LIKE

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!

share|improve this answer
add comment

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.

share|improve this answer
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. –  LaceySnr Feb 11 '13 at 23:41
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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