function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

SOQL Query for Case Emails - How?

I am trying to do a relationship query for EmailMessages associated with a Case.  However, something seems to be "special" about this relationship.

Normally, I can retrieve related lists like this:

SELECT id, (SELECT CommentBody FROM Case.CaseComments) FROM Case WHERE id = '50050000003DZRB'

However, this approach doesn't seem to work with EmailMessages.  I don't see any related list for EmailMessages in the Enterprise WSDL, but surprisingly, a relatinship does show up in the schema browser Apex Toolkit for Eclipse.  However the relationship looks "funny" there - you can't drill into it like you can with the other Case relationships (it has a blue ball next to it instead of a plus sign), so you can't view the relationship name.  For the sake of trying everything, I experimented with some guesses at the relationship name -

(SELECT id FROM Case.EmailMessages)
(SELECT id FROM Case.CaseEmailMessages)
(SELECT id FROM Case.Emails)
(SELECT id FROM Case.CaseEmails)

None of these worked.  But it feels like this should be doable - the EmailMessage object has a "ParentID" field that points back to the Case, so I can always do a separate SOQL query on EmailMessage.  But I'd hoped to use a relationship query here, for various reasons.

Can anyone explain the apparently "special" relationship from Case to EmailMessage?
Well, the above query now works in SFDC.  So I guess something got fixed.  :)
Hi, Can you please let me know on how you got the query worked? 
I am trying something like "lstCase = [select Id, (select id from EmailMessage) from Case where Id =: caseId];" and I get a error saying "Relationship not understood". 
Please help me with this.
Glenn WeinsteinGlenn Weinstein
Try adding an "s" at the end of "EmailMessage", to name the collection.
lstCase = [select Id, (select id from EmailMessages) from Case where Id =: caseId];