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
bohemianguy100bohemianguy100 

Relationship query - traverse Event down thru the WhatId

I am trying to query the Event object and traverse down thru the relatedTo (whatId) object and its corresponding fields. I was trying the following query in Eclipse:

 

Select e.What.Account_Name__c, e.What.Project__c, e.What.Confirmed_Start_Date__c, e.What.Account__c, e.WhatId, e.Account.Name, e.AccountId From Event e

 

I get the following error: No such column 'Account_Name__c' on entity Name.  The field exists on a custom object Project_Request__c that is the relatedTo field on the Event.

 

How can I query the Project_Request__c object that is the relatedTo id on the event and access the fields on the Project_Request__c object from the Event object?

 

Thanks for any help.

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

The Who and What fields on the Task/Event objects are polymorphic fields - i.e. they can point to one of many different types of SObjects. Unfortunately you can't access any fields from a relationship field that is poloymorphic in SOQL. You'll have to meet your requirement with 2 SOQL queries. First query the Event object and make a Set of all 'WhatId' values. Then make another query to the Project_Request__c object for those set of records. Hope this helps.

All Answers

forecast_is_cloudyforecast_is_cloudy

The Who and What fields on the Task/Event objects are polymorphic fields - i.e. they can point to one of many different types of SObjects. Unfortunately you can't access any fields from a relationship field that is poloymorphic in SOQL. You'll have to meet your requirement with 2 SOQL queries. First query the Event object and make a Set of all 'WhatId' values. Then make another query to the Project_Request__c object for those set of records. Hope this helps.

This was selected as the best answer
shruthishruthi

Hi

 

I believe, one cannot traverse down on activities (tasks or events).

 

Rather, you can use two queries. One query [on Event] to get the whatId and another query to get the required fields on the object [Project_Request__c] using the whatid.

 

Regards

Shruthi