Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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]
share|improve this question
    
Anything can go inside a for loop, but if it's in a trigger you're going to hit governor limits. –  Ralph Oct 11 '12 at 3:30
add comment

2 Answers 2

up vote 3 down vote accepted

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
  }
share|improve this answer
    
What about how to use the results of the query in apex? –  Daniel Blackhall Oct 11 '12 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 –  Mohith Shrivastava Oct 11 '12 at 4:38
    
can you post an example in your answer? and delete the duplicate url while you're at it –  Daniel Blackhall Oct 11 '12 at 4:55
    
Done and you can check.Thanks for notifying –  Mohith Shrivastava Oct 11 '12 at 5:01
    
The one thing I really wanted to see, you left out :) how do I refer to the Folder.Name or User.Id in code? –  Daniel Blackhall Oct 11 '12 at 5:24
show 2 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.