3

Some of our Events are related to Opportunities. Some of our Opportunities are related to Accounts which are PersonAccounts. Some of our Person Accounts have a value for PersonMobilePhone.

Is there some way I can do something like:

List<Event> eventList = [
                SELECT StartDateTime
                FROM Event
                WHERE StartDateTime >= :targetDate
                        AND StartDateTime < :dayAfterTargetDate
                        AND RelatedRecord.SObjectType = Opportunity.SObjectType 
                        AND RelatedRecord.Account.PersonMobilePhone != NULL
        ];
4

You can't do it for most fields. As it's a polymorphic relationship field, you can utilize Type to get the object of a polymorphic relationship, but you're limited in what fields are accessible through that key.

SELECT Id,WhatId 
FROM Event 
WHERE What.Type IN ('Opportunity')

This is highlighted in the Name object documentation.

Non-queryable object that provides information about foreign key traversals when the foreign key has more than one parent.

This object is used to retrieve information from related records where the related record may be from more than one object type (a polymorphic foreign key).

The only available fields are those listed in the above Name object documentation. As such, you won't be able to go to the Account from the polymorphic field.

You can also check which fields are available for accessing by running a describe on a field and seeing if namePointing is true. Below is an example of looking at the Phone on the Account standard object.

enter image description here

1

While @Kris Goncalves's accepted answer above is technically correct, it misses one imporant thing regarding the intention of the question.

Event has a standard SObjectfield field Event.AccountId. When we write Apex, this field is read-only, but it seems to be magically and reliably populated with the value of Opportunity.AccountId when What.Id points to an Opportunity.

Moreover, the value of Event.Account will be what we should expect when the value of Event.AccountId is populated.

So the following would give the desired result:

List<Event> eventList = [
                SELECT StartDateTime
                FROM Event
                WHERE StartDateTime >= :targetDate
                        AND StartDateTime < :dayAfterTargetDate
                        AND What.Type IN ('Opportunity')
                        AND Event.Account.PersonMobilePhone != NULL
        ];
3
  • 2
    Details on how exactly AccountId is populated on Task and Event are here (under AccountId).
    – David Reed
    Oct 5 '20 at 23:19
  • @DavidReed, cheers for additional info. Does "Custom object that is a child of Account" require a Master-Detail relationship, or can that work with Lookup too? Oct 6 '20 at 6:00
  • 1
    I don't know off the top of my head but I would imagine it is master-detail only, otherwise it could be ambiguous.
    – David Reed
    Oct 6 '20 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.