function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sh-at-youseesh-at-yousee 

Is it possible to use SOQL LIKE with IDs?

Hi all,

 

I'm just wondering if it is possible to use SOQL LIKE with IDs? I tried the following but without any luck.

 

 

List<Case> cases = [SELECT Id FROM Case WHERE Id LIKE '005%'];

 

 

What I'm trying to achieve is to fetch all cases owned by a user (and therefore not a queue). I know I could just fetch all active users and use IN but that would esentially create another query towards the database because I'd first have to fetch the users and then fetch the cases.

 

My current solution is this:

 

 

List<Case> cases = [SELECT Id, OwnerId, Urgent__c FROM Case WHERE (Owner.Profile.Name LIKE '%YFF%' OR Owner.Profile.Name LIKE '%YK%' OR Owner.Profile.Name LIKE '%YO%' OR Owner.Profile.Name LIKE '%YB%') AND OwnerId != '005200000013dtq' AND LastModifiedDate > :lastUpdated AND Reserve_Until__c = null];

 

I'm not too keen on this approach because I'd have to update this SOQL whenever a new profile should be added to the list which brings me back to my initial approach.

 

 

All suggestions are welcome.

 

Cheers.

 

/Søren Nødskov Hansen

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

According to the docs:

 

--- 8< ---

The LIKE operator is supported for string fields only.

--- 8< ---

 

which is a shame, as it would be useful.

All Answers

bob_buzzardbob_buzzard

According to the docs:

 

--- 8< ---

The LIKE operator is supported for string fields only.

--- 8< ---

 

which is a shame, as it would be useful.

This was selected as the best answer
sh-at-youseesh-at-yousee

Hi Bob,

 

Thanks for the quick reply. I must have missed that in the documentation. Sorry about that.

 

And, yes, it would be very useful.

 

/Søren Nødskov Hansen

Anup JadhavAnup Jadhav

Alternatively you can fetch the owner first using the name, and then use the owner.id in the soql query. This approach will also prevent updating the id when you deploy your code from dev to test to live.

 

- A J

SuperfellSuperfell

select id from case where owner.type = 'user'

sh-at-youseesh-at-yousee

Even better. Thanks, Simon.

 

/Søren Nødskov Hansen

Maarten Dun 6Maarten Dun 6
For anyone who is still looking for a solution; Create a formula field type Text and link to the ID field. Use the formula field for your SOQL, which handles the Like expression without problems.