How to get Email from Task object record using SOQL

Andrew Jackman picture Andrew Jackman · Feb 4, 2013 · Viewed 7.7k times · Source

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.

Answer

amrcn_werewolf picture amrcn_werewolf · Feb 4, 2013

Because the WhoId and WhatId fields of Task are polymorphic (i.e. can point to many different kinds of objects) you can't just query relationships through them like you can for normally-related objects. Instead, you'll have to do 2 SOQL queries, the first to get the Task information and the second to get the info from the Contact or Lead that the Who is pointing to.