Search code examples
salesforcesoql

How to get Email from Task object record using SOQL


I am trying to get the Contact/Lead email from the Task object using SOQL (I am creating an interface in PHP to back up messages with a specific subject). Here is my query right now:

SELECT Subject,Who.FirstName,Who.LastName,Who.Email,Who.Phone,Description FROM Task

This works/doesn't throw an error and gives me results, but Who.Email is always empty (and, coincidentally Who.Phone is as well, but it is not very important for this). If I try just using Email I get an error that the field doesn't exist, which is weird because Email is under Task Fields as a standard field.

I have also tried several google searches with no sort of assistance found.


Solution

  • The SOQL Polymorphism article linked to by Zach above works perfectly.

    SELECT Subject,
      TYPEOF Who
        WHEN Lead THEN Email
        WHEN Contact THEN Email
      END
    FROM Event