Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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.

1 Answer

0 votes
by (32.1k points)
edited by

This is because the WhoID and WhatID fields of the task are polymorphic (i.e. they 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 one, to get the Task information and the second one, to get the info from the Contact or Lead that the Who is pointing to.

Enroll in our Best Salesforce Course to get a professional certification!

Related questions

0 votes
1 answer
asked Dec 14, 2020 in Salesforce by dante07 (13.1k points)
0 votes
2 answers
asked Jul 15, 2019 in Salesforce by Kartik12234 (11.9k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...