5

I am unable to obtain the Name of a Parent object, where the child is CombinedAttachments.

The SOQL I use is listed below and works as expected.

Select (Select Id, Title, RecordType, LastModifiedDate, CreatedBy.Name, ParentId    
        From CombinedAttachments    
        Where ParentId = '001b0000009ovxS' 
        Order By LastModifiedDate DESC 
        Limit 100)
From Account 
Where Id = '001b0000009ovxS'

What is impossible is to add a filed like Parent.Name or Parent.Account.Name. However, in other cases of Parent-Child queries like that, it is straightforward to refer to Parent fields by using dot notation like Parent.CaseNumber or Who.Name It seems that due to the fact that there may be multiple references possible in ParentID (Account,Asset,Campaign,Case,Contact,Contract,EmailTemplate,Event,Lead,Opportunity,Product2,Solution,Task)

3 Answers 3

1

This is an example of a Polymorphic relationship. I don't have this feature enabled, so cannot confirm, but reading through the docs it's likely still that your requirement to obtain this information within the same SOQL will not be resolved by this feature either. Though it does allow you to determine in Apex the type, such that you could perform additional query/s after you group the ParentId's by type.

TYPEOF is currently available as a Developer Preview as part of the SOQL Polymorphism feature. For more information on enabling TYPEOF for your organization, contact salesforce.com.

I suspect the reason being, is that despite Name being quite a common field, it's not quite common enough (there are a few standard objects that don't have it) to be permitted when de-referencing a polymorphic relationship. Only really Salesforce could explain why they don't support this i guess.

0

In addition to Andrew's answer I'd like to add a sample of SOQL using the TypeOf.

I faced the same issue time ago when I was trying to get different fields from Task's Parent depending of the parent type. In my case the parents were custom object.

Sample:

SELECT 
TYPEOF What
    WHEN ObjectA__c THEN fieldA__c, fieldB__c
    WHEN ObjectB__c THEN fieldC__c
    ELSE Name
END
FROM Task

This is other standard sample using Object.Type:

SELECT 
TYPEOF What
    WHEN Opportunity THEN Name, amount
    WHEN Account THEN Name, AnnualRevenue  
END
FROM Task
WHERE 
   What.Type IN ('Opportunity','Account')
0

One possible solution is to put the Parent field at the parent part, rather the sub-query:

 SELECT Name, Id, ( SELECT Id, Title, RecordType, LastModifiedDate,
 CreatedBy.Name, ParentId from CombinedAttachments WHERE ParentId =
 '001b0000009ovxS' ORDER BY LastModifiedDate DESC limit 100 ) FROM
 Account WHERE Id = '001b0000009ovxS'

You must log in to answer this question.

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