Print this page

SOQL TYPEOF expressions

Knowledge Article Number 000175996
Description
While running SOQL Polymorphic queries receiving the below error :
 
Error received : MALFORMED_QUERY: SOQL TYPEOF expressions are not supported in this organization
 
Sample query :
 
SELECT Subject,
  TYPEOF What
    WHEN Account THEN Phone, NumberOfEmployees
    WHEN Opportunity THEN Amount, CloseDate
  END
Resolution
SOQL Polymorphism was introduced as a developer preview feature in Winter ’13.
 
With soql polymorphism, you can perform multiple operations in a single query which would have taken multiple queries, thereby reducing the amount of code you need, and making your code easier to understand and manage.
 
A polymorphic relationship is a relationship where the referenced objects can be one of several different object types. For example, the What relationship field in an Event can reference an Account, or a Campaign, or an Opportunity. The LastModifiedBy relationship field in an Event, however, can only reference a User and is not polymorphic.
 
Without soql polymorphism, you have to do extra work when making queries that involve polymorphic relationships. Since you don’t know in advance what type of object is being referenced, you might have to get the ID of a Name object instead, process this information in some logic code, and then do a second query using information from that Name object to get to the content of the referenced object. soql polymorphism lets you do all of this work in a single query.

soql polymorphism example using TYPEOF
 
With soql polymorphism you can provide instructions directly in the soql query on what to do for each possible type of object. You do this via the new TYPEOF clause. Here is an example:
 
SELECT Subject,
  TYPEOF What
    WHEN Account THEN Phone, NumberOfEmployees
    WHEN Opportunity THEN Amount, CloseDate
  END
FROM Event

Currently, this feature is only available in sandbox environment, This feature is not available in production organization.
Please contact Support to get this feature enabled in your sandbox organization if you would like to test it.
Once this feature is generally available (GA), support will be able to enable this in production (paid) organizations too.


 




promote demote