I'm writing a short snippet of code to get which records of a certain type were approved, and by who. However, I don't have an id list- I have an sObject type. I know I can filter the id values after the query with something like:

if (String.valueOf(processInstance.TargetObjectId).startsWith('006')) { ... }

But, I'd rather filter these values out in a query instead of having to do it in apex. I tried using the LIKE keyword to filter the query, as below:

SELECT Status, TargetObjectId FROM ProcessInstance WHERE TargetObjectId LIKE '006%'

But this query fails with:


[object Object]: TargetObjectId FROM ProcessInstance WHERE TargetObjectId LIKE '006%' LIMIT ^ ERROR at Row:1:Column:58 invalid operator on id field

I know the docs say the LIKE operator can only be used with String types, but I was under the impression that the Id type was just a wrapper over the String type. Is there any way I can get this filter working inside a soql query?

  • are you open to using an IN operator, you can prepare a list of records and use TargetobjectId IN : List_of_Ids
    – Rao
    Jul 22, 2016 at 17:25
  • That would require me to run a second query before this one- which is an option, but the only fields I need are the TargetObjectId and the Status from the ProcessInstance, so I'd rather construct the objects from the single query. Jul 22, 2016 at 17:31

1 Answer 1


Use TargetObject.Type instead.

SELECT Status, TargetObjectId FROM ProcessInstance WHERE TargetObject.Type = 'Opportunity'

See Understanding Polymorphic Keys and Relationships:

Using the Type qualifier

You can use the Type qualifier on a field to determine the object type that’s referenced in a polymorphic relationship. Use the Type qualifier in the WHERE clause of a SELECT statement to conditionally control what’s returned from the query depending on the referenced object type. The following SELECT statement uses Type to filter the query based on the What field in Event.

FROM Event
WHERE What.Type IN ('Account', 'Opportunity')

At run time this SELECT statement returns the IDs for Events that reference Accounts or Opportunities in the What field. If an Event referenced a Campaign in the What field, it wouldn’t be returned as part of this SELECT. Unlike the TYPEOF expression, object types are returned as strings from Type. You can apply any WHERE comparison operator to the object type strings, such as = (Equals) or LIKE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .