2

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]
1
  • Anything can go inside a for loop, but if it's in a trigger you're going to hit governor limits. Commented Oct 11, 2012 at 3:30

3 Answers 3

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
  }
6
  • What about how to use the results of the query in apex? Commented Oct 11, 2012 at 1:44
  • @DanielBlackhall the results as i said will be of List<Object> type and like normal queries we can use in for loop too Commented Oct 11, 2012 at 4:38
  • can you post an example in your answer? and delete the duplicate url while you're at it Commented Oct 11, 2012 at 4:55
  • The one thing I really wanted to see, you left out :) how do I refer to the Folder.Name or User.Id in code? Commented Oct 11, 2012 at 5:24
  • @DanielBlackhall Thanks for bringing to notice updated the information and answer Commented Oct 11, 2012 at 6:03
0

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.

0

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);
}

You must log in to answer this question.

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