I'm trying to construct a query on Event to retrieve some custom fields on Opportunity in the same query as the Event fields. The "WhatID" is the Opportunity.Id. The WhoID is unrelated to the Account. The OwnerID owns the Opportunity.

I keep getting the error message that Apex doesn't understand the relationship of Opportunity, Opportunity__r, Opportunities, Opportunities__r, etc., regardless of what I seem to do; including enclosing that portion in a separate sub select.

Here's what I want to select:

Assnmnt = [select Id, OwnerID, Owner.Name, Subject, ActivityDateTime, ActivityDate, DurationInMinutes, StartDateTime, EndDateTime, Description, WhatID, WhoID, Who.Name, What.Opportunity.Account_Address__c, What.Opportunity.Account.Name, What.Opportunity.Account_Phone__c, Opportunty__r.Customer_Contact__c from Event Where Id =: Asn2Create and WhatId =: What.Opportunity.Id];

Any help on how to properly construct this to work would be appreciated.

  • Have you tried to create the query in eclipse or Force.com Explorer? You will get the relationship names automatically over there. Commented Aug 6, 2013 at 13:06
  • Yes I have. I tried Ajax tools, the equiv of Force.com Explorer. It doesn't take me "up" to Opportunity, only to Accounts of which Opportunity is a child. Opportunity is the parent of the Event. Problem is, Apex doesn't know that because What can be a number of things and I have no way of telling it that or don't know how to!
    – crmprogdev
    Commented Aug 6, 2013 at 13:09

1 Answer 1


In the query example you provided above, "What" is already the Opportunity, you don't need to "drill-in" to a reference off of that field. So, you should be able to access Opportunity fields using What.[Opportunity-Field-Name] as long as the fields you are querying are Standard fields.

To query custom fields from a related object on Tasks or Events, you could use TYPEOF in a SOQL statement, but you'd need to contact Salesforce to get it enabled first.

I'm not entirely sure if this would work because I don't have TYPEOF enabled, but you could try this:

// where getOpportunityId is a method that returns the Opportunity Id
My_Opportunity_ID = getOpportunityId(); 

Assnmnt = [
        Id, OwnerID, Owner.Name, Subject, ActivityDateTime, ActivityDate, 
        DurationInMinutes, StartDateTime, EndDateTime, Description,  WhatID, WhoID, 
        TYPEOF What 
            WHEN Opportunity THEN Account_Address__c, Account.Name, 
                 Account_Phone__c, Customer_Contact__c 
    FROM Event 
    WHERE Id = :Asn2Create 
    AND WhatId = :My_Opportunity_ID
    AND What.Type = 'Opportunity'
  • I had considered trying something like: system.debug(whatID + ' = ' + Opportunity.get(WhatId)); or similar, but hadn't figured out what it should look like until your post. What you've suggested is giving me the following error message: No such column 'Account_Address__c' on entity 'Name'. It appears this is close, but I think we now have Opp name and not Opportunity.
    – crmprogdev
    Commented Aug 6, 2013 at 14:45
  • Ah, that makes sense actually. The "What" field is a polymorphic key (references multiple types of objects). If I remember correctly, custom fields aren't available from polymorphic key fields.
    – Matt K
    Commented Aug 6, 2013 at 15:52
  • Thanks for trying. Good info to know though for future use. :) Will work around it either using another query or doing string ops on other fields in the event; something I was trying to avoid.(sigh)
    – crmprogdev
    Commented Aug 6, 2013 at 15:58

You must log in to answer this question.

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