How to get email from a task object record using SOQL

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

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

This works / does not give an error and gives me the results, but Who.Email always empty (and, coincidentally, Who.Phone too, but this is not very important). If I try to use only Email , I get an error that the field does not exist, which is strange because Email is under the Tasks fields as a standard field.

I also tried several google searches without any help.

+4
source share
4 answers

Since the WhoId and WhatId fields of the task are polymorphic (that is, they can point to many different objects), you cannot simply request relationships through them, as you can, for objects associated with objects. Instead, you will need to execute 2 SOQL queries, the first to get information about the task, and the second to get information from Contact or Lead, which is pointed out by who.

+1
source

Contact Salesforce Support to enable Polymorphic SOQL in your organization, after which you can determine if it leads or contacts it. This is in the developer preview, but it is pretty neat. Here's a usage example:

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 comment "TODO: rewrite this" in your code;)

+2
source

As mentioned earlier, the WhoId and WhatId fields in the task are polymorphic (meaning that they can point to multiple targets. If you don't have SOQL Polymorphism , as @eyescream describes, the only fields you can request are those that are on the 'Name' object, but there are exceptions that are described in this document.

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

This will return data for FirstName and LastName if Contact / Lead has these fields, but as stated in the above document, it will never return results for Who.Email, since none of the two possible Who (Contact, Lead) targets is a user object.

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

If you use Queues with a Case object, the owner of the case can be either a user or a group (the record of which is a type). Since both Name and Email are in the Name object, the above request always returns data for the 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 "Name" field of the Case object is actually CaseNumber , and not the name --- there are several other standard objects with "non-standard" (ironic) name fields, for example. SolutionTitle, and then there are others that don't even have Name fields, like CampaignMember . HOWEVER, one of Salesforce’s great features with polymorphic fields technically points to the Name object that the above query will return results for Cases! If Case CaseNumber is 00001234, then What.Name in the above query will return 00001234, while for the account it will return the account name, and for the solution it will return SolutionTitle.

It is also useful to note here that you can limit which targets are returned by filtering in the Type field, which is a special field in the Name object.

+2
source

Try this in the console to get the task / event value

List e1 = new list (); e1 = [select Id, whoId from event WHERE id = '00U9xxxxxxxxxxx']; System.debug ('@@@@@' + e1);

Set whoId = new Set (); for (event e2: e1) {whoId.add (e2.whoId); } list c = [select name, phone, email address from contact where Id IN: whoId]; System.debug ('@@@@@' + s);

END ---------------- -------------

thanks

0
source

All Articles