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
Prasan6Prasan6 

How to get parent field values using soql for tasks

Hi guys, 

I am running a soql query and I need to get parent (custom object's) field values in the sql. My soql is like this 

select status, subject, whatid, what.name, what.status__c from task

I get the error 
No such column 'status__C' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

how to get the parent field value from task?

Thanks, 
Prasanna
 
Bryan JamesBryan James
The issue you are most likely having is due to the field in question WhatId being polymorphic. Since it has multiple parent objects it can look up to there are limits as to which fields it can access on those parent object. First off it can only reference standard fields, no custom ones. Secondly all the objects must contain the standard field, such as Name.  
Amit GhadageAmit Ghadage
Hi Prasanna,
Task cannot have  master detail relationship. So you cant access CustomObjects field by querying On Task.

do one thing
make set of whatId
Set<Id> customObjectIdSet = new Set<Id>();

for(Task t : [SELECT WhatId,WhoId FROM Task])
customObjectIdSet.add(t.whatId);

Then Query on CustomObjects
List <customObject > customObject List = [Select status__c ,name from customObject where Id IN :customObjectIdSet]

Best Regards,
Amit Ghadage.
 
Prasan6Prasan6
Hi Bryan,
Thanks for explaing why it is not possible. 

Hi Amit, 
Thanks for giving code snippet. 

I wanted to avoid two soql, unfortunately because of Task limitations I ended up having two soql.