Find your content:

Search form

You are here

SOQL Polymorphism Results


What kind of result set is returned from this SOQL and can I write this inside a for loop?

[SELECT Name, Description,
    TYPEOF OwnerId
        WHEN Folder THEN Name
    FROM Report]

Attribution to: fourq

Possible Suggestion/Solution #1

You need to use the SObject interface to dereference. I have not found a way to do it statically.

final List<Report> theReports=[SELECT OwnerId, Name, Description,
    TYPEOF Owner
        WHEN Folder THEN Name
        WHEN Organization THEN Id
        WHEN User THEN Id
    FROM Report
    LIMIT 20];

for(Report current :theReports){
  // Refer to Owner by SObject
  final SObject theOwner = current.Owner;

  // Be careful to check "isSet()"
  // otherwise: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.Name
  final String theOwnerName = theOwner != null && theOwner.isSet('Name') ? (String) theOwner.get('Name') : null;
  final Id theOwnerId = theOwner != null && theOwner.isSet('Id') ? (Id) theOwner.get('Id') : null;

  // Do something clever to ensure you use the right field  
  final String theOwnerLabel = theOwnerName != null ? theOwnerName : theOwnerId;

  // Prove it worked
  System.debug('Owner: ' + theOwnerLabel + ' ' + current);

Attribution to: Bobby White

Possible Suggestion/Solution #2

I try the suggested query in a SOQL query tool such as the Developer Console or SOQLExplorer and I get this error message.

SOQL TYPEOF expressions are not supported in this organization

Searching for cause leads to:

Is SOQL Polymorphism enabled by default in API 26?

Which takes you to here:

Which says this is a feature that needs to be enabled by contacting SF.

Attribution to: Bryan

Possible Suggestion/Solution #3

This is a polymorphic query recently introduced by platform

Since Report ownerId can belong to Folder,organisation or user in Report Object now using TYPE OF gives flexiblity to query .Now If its folder Type then return name

same say you had one more Organisation in query since ownerId can refrence to organisation too .


[SELECT Name, Description,
    WHEN Folder THEN Name
    WHEN Organistion THEN ID
FROM Report]

Now if owner Id is Folder the query will select Name If owner ID id user the Id is returned in the query

The end result will be Report object type and yes you can have in for loop.

Check the documentation for more info

List<rReport> lstrep=[SELECT Name, Description,
    WHEN Folder THEN Name
    WHEN Organistion THEN ID
FROM Report]

 for(Report r:lstrep){
  //Say to refer folder name
  String n=r.Owner.Name;
  Id xyz=r.Owner.Id;//If it is fetched from the Folder this will be null

  //do processing

Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content