Find your content:

Search form

You are here

SOQL Polymorphism Results

 
Share

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
    END
    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
    END
    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: http://blogs.developerforce.com/tech-pubs/2012/09/soql-polymorphism-or-how-i-learned-to-love-the-polymorphic-relationship.html

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 Force.com 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 .

eg:

[SELECT Name, Description,
TYPEOF Owner
    WHEN Folder THEN Name
    WHEN Organistion THEN ID
    WHEN User THEN ID
END
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

http://www.salesforce.com/us/developer/docs/soql_sosl/index_Left.htm#StartTopic=Content/sforce_api_calls_soql_relationships.htm#relates_and_polymorph_keys

List<rReport> lstrep=[SELECT Name, Description,
TYPEOF Owner
    WHEN Folder THEN Name
    WHEN Organistion THEN ID
    WHEN User THEN ID
END
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 https://salesforce.stackexchange.com/questions/2171

My Block Status

My Block Content