-2

I would like a list of all task/events which are concerning to a account record and and a contact record and the contact has a specific recordtype

I have the whoid, but how can I query the recordtype of the contact?

select Id, Subject, CreatedDate, StartDateTime, Createdby.Name, Owner.Name from Event where AccountId =: currentRecordId and Contact.RecordTypeId = 'XXXXXXXXXXXXXXX'

2 Answers 2

3

To find just contacts that match a given record type, use a subquery filter:

SELECT ... 
FROM Event 
WHERE AccountId = :currentRecordId AND 
WhoId IN
  (SELECT Id FROM Contact WHERE RecordType.Name = 'ABC')

You can use either the Name or the RecordTypeId, as in your example, whichever you prefer.

2

WhoId is a polymorphic relationship field (it can point to a Contact or a Lead).

The documentation on Understanding relationship fields and polymorphic fields tells us that we can use TYPEOF to query related data for such fields.

e.g.

SELECT
    Id,
    TYPEOF Who
        WHEN Contact THEN RecordType.Name
    END
FROM Event
WHERE
    Who.Type = 'Contact'

Accessing the related data in Apex is then just like anything else. myEvent.Who.RecordType.Name

It doesn't appear that we can access that related data in the WHERE clause though, so that part of your filtering would need to be done in Apex.

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