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
Michael MMichael M 

SOQL query on feeditem's parent's custom fields

Hello, I need to query a feeditem's parent's custom field.

Here is so far:
  
            Discharge__c discharge=[Select Id, Name, Date_of_Birth__c from Discharge__c where Id = :this.dis.id];
        
        listofNotes = [SELECT ID, CreatedDate, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, ParentId, Parent.Name, Body
                      FROM feeditem 
                      WHERE Parent.Name = :discharge.name ];

I need to add another WHERE clause stating that the Parent (discharge record's) date of birth  is = discharge.date_of_birth__c

Is this possible?
Best Answer chosen by Michael M
Alain CabonAlain Cabon

ParentId in FeedItem is a Polymorphic Field associated with dozens of objects.

Name is an exception among the fields because it belongs to all the linked objects here.

Unfortunately, BirthDate from Contact is not allowed in the filter part of the request but just in the select part.

And you cannot use  WHERE Parent.Name = :discharge.name either but its equivalent only:  ParentId ='0030Y00000AMgxNQAT'
 
SELECT id,
TYPEOF parent WHEN Contact THEN name,birthdate END
FROM feeditem
WHERE Parent.Type= 'Contact' and ParentId ='0030Y00000AMgxNQAT' // redondant just for the sample
  • TYPEOF is only allowed in the SELECT clause of a query. You can filter on the object type of a polymorphic relationship using the Type qualifier in a WHERE clause, see Filtering on Polymorphic Relationship Fields for more details.
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_polymorph_keys.htm

Therefore, there is a list of Feed items that is browsed to verify the birthdate.
List<FeedItem> filist = [select id .... from feedIem where ... ];
for(FeedItem fi:filist){
    if(fi.Parent instanceof Contact){
        Contact cnt = fi.Parent;
        System.debug('cnt.BirthDate=' + cnt.BirthDate + ' cnt.name:' + cnt.Name);
    }
}

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Michael,

I suspect your query is answered in this blog,

https://salesforce.stackexchange.com/questions/148079/how-to-get-custom-object-with-related-item-feed-in-soql

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Michael MMichael M
I don't see how that applies to my question..
Alain CabonAlain Cabon

ParentId in FeedItem is a Polymorphic Field associated with dozens of objects.

Name is an exception among the fields because it belongs to all the linked objects here.

Unfortunately, BirthDate from Contact is not allowed in the filter part of the request but just in the select part.

And you cannot use  WHERE Parent.Name = :discharge.name either but its equivalent only:  ParentId ='0030Y00000AMgxNQAT'
 
SELECT id,
TYPEOF parent WHEN Contact THEN name,birthdate END
FROM feeditem
WHERE Parent.Type= 'Contact' and ParentId ='0030Y00000AMgxNQAT' // redondant just for the sample
  • TYPEOF is only allowed in the SELECT clause of a query. You can filter on the object type of a polymorphic relationship using the Type qualifier in a WHERE clause, see Filtering on Polymorphic Relationship Fields for more details.
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_polymorph_keys.htm

Therefore, there is a list of Feed items that is browsed to verify the birthdate.
List<FeedItem> filist = [select id .... from feedIem where ... ];
for(FeedItem fi:filist){
    if(fi.Parent instanceof Contact){
        Contact cnt = fi.Parent;
        System.debug('cnt.BirthDate=' + cnt.BirthDate + ' cnt.name:' + cnt.Name);
    }
}
This was selected as the best answer