Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
Andrew Jackman Avatar asked Feb 04 '13 18:02

Andrew Jackman


3 Answers

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.

like image 60
amrcn_werewolf Avatar answered Sep 20 '22 00:09

amrcn_werewolf


Ask Salesforce Support to enable "Polymorphic SOQL" in your organization, then you'll be able to determine whether it's Lead or Contact. It's in Developer Preview at the moment but it's pretty neat. Here's sample usage:

http://blogs.developerforce.com/tech-pubs/2012/09/soql-polymorphism-or-how-i-learned-to-love-the-polymorphic-relationship.html

Or at least leave a "TODO: rewrite this" comment in your code ;)

like image 22
eyescream Avatar answered Sep 21 '22 00:09

eyescream


As mentioned before, the WhoId and WhatId fields on Task are Polymorphic (meaning that they can point to multiple target objects. Unless you have the SOQL Polymorphism feature enabled, as @eyescream describes, the only "related" fields that you can query for are those that are on the 'Name' API object, but there are exceptions, which are documented on this document.

Example 1: select Who.FirstName, Who.LastName, Who.Email from Task

This will return data for FirstName and LastName if the Contact/Lead has those fields populated, but, as the above document says, it will never return results for Who.Email, because neither of the two possible target objects of the Who field (Contact,Lead) is the User object.

Example 2: select Owner.Name, Owner.Email from Case

If you are using Queues with the Case object, then a Case's Owner can be either a User or a Group (of which a Queue is a type) record. Because both Name and Email are on the Name object, the above query will always return data for Name and, if the target object is a User, it WILL return data for Owner.Email.

Example 3: select What.Name, WhatId from Task where What.Type in ('Case','Account','Solution')

This is a very interesting example because the Case object's "Name" field is actually CaseNumber, not Name --- there are a couple other standard objects with "non-standard" (ironic) Name fields, e.g. SolutionTitle, and then there are others that don't even have Name fields, like CampaignMember. HOWEVER, one of the great things about Salesforce having polymorphic fields technically point to the Name object is that the above query WILL return results for Cases! If a Case's CaseNumber is 00001234, then What.Name in the above query will return 00001234, whereas for an Account it will return the Account's Name, and for a Solution it will return SolutionTitle.

It is also helpful to observe here that you CAN limit which target objects are returned by filtering on the 'Type' field, which is a special field on the Name object.

like image 44
zachelrath Avatar answered Sep 19 '22 00:09

zachelrath