TYPEOF
TYPEOF is an optional clause that can be used in a SELECT statement of a SOQL query when you’re querying data that contains polymorphic relationships. A TYPEOF expression specifies a set of fields to select that depend on the runtime type of the polymorphic reference.
TYPEOF is available in API version 46.0 and later. It is also available in earlier versions as part of a Developer Preview of the SOQL Polymorphism feature.
If you need to query multiple polymorphic relationship fields, you can use more than one TYPEOF expression in a single SELECT statement.
You can provide as many WHEN clauses as needed, one per object type. The ELSE clause is optional and used if the object type for the polymorphic relationship field in the current record doesn’t match any of the object types in the provided WHEN clauses. The syntax specific to TYPEOF is as follows.
| Syntax | Description |
|---|---|
typeOfField | A polymorphic relationship field in objectType or a polymorphic field in a parent of objectType that can reference multiple object types. typeOfField cannot reference a relationship field that is also referenced in the fieldList of a SELECT statement. |
whenObjectType | An object type for the given WHEN clause. When the SELECT statement runs, each object type associated with the polymorphic relationship field specified in the typeOfField expression is checked for a matching object type in a WHEN clause. |
whenFieldList | A list of one or more fields, separated by commas, that you want to retrieve from the specified whenObjectType. These are fields in the referenced object type or paths to related object fields, not fields in the primary object type for the SELECT statement. |
elseFieldList | A list of one or more fields, separated by commas, that you want to retrieve if none of the WHEN clauses match the object type associated with the polymorphic relationship field specified in typeOfField. This list may only contain fields valid for the Name object type, or paths to related object fields in Name. |
Note the following considerations for using TYPEOF:
-
TYPEOFcan’t be used with a relationship field whosenamePointingattribute is false. -
TYPEOFcan’t be used with a relationship field whoserelationshipNameattribute is false. -
TYPEOFis only allowed in theSELECTclause of a query. You can filter on the object type of a polymorphic relationship using theTypequalifier in aWHEREclause. For details, see Filter on Polymorphic Relationship Fields. -
TYPEOFisn’t allowed in queries that don’t return objects, such asCOUNT(). -
TYPEOFcan’t be used in SOQL queries that are the basis of Streaming API PushTopics. -
TYPEOFcan’t be used in SOQL used in Bulk API. -
TYPEOFexpressions can’t be nested. For example, you can’t useTYPEOFinside theWHENclause of anotherTYPEOFexpression. -
TYPEOFisn’t allowed in theSELECTclause of a semi-join query. You can useTYPEOFin theSELECTclause of an outer query that contains semi-join queries. The following example is not valid becauseTYPEOFis used in the semi-join query:The following example is valid because
TYPEOFis only used in the outerSELECTclause: -
TYPEOFcan’t be used in queries with functions in theSELECTclause. The following example is not valid because theTYPEOFincludes theFORMATfunction.Instead, run the same query without functions to retrieve a list of IDs.
Then, run a second query with functions on the resulting ID list.
-
TYPEOFcan’t be used in queries withGROUP BY,GROUP BY ROLLUP,GROUP BY CUBE, andHAVING.
The following example selects specific fields depending on whether the What field of an Event references an Account or Opportunity.
See Understanding Relationship Fields and Polymorphic Fields for details on polymorphic relationships, and more examples of TYPEOF.