3

How to get the parent object field from Task standard object using relationship query. select whatid,what.Email__c from Task. Its showing error. How can I get the email field from various object which is related to Task. If anyone knows help.

2 Answers 2

2

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. The only "related" fields that you can query are those that are on the 'Name' API object.

1
  • Thank u..Its fine.. But using this we can querying the common field of various object. But my requirement is to get the custom email field from various object which is related to Task. This is my requirement. Aug 13, 2013 at 6:49
2

Building off what brovasi said, WhoId and WhatId fields are polymorphic, you can't just query relationships through them like you can for normally-related objects.

you will need to do 2 SOQL queries to accomplish this, 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.

Example:

Task t = [Select Id, Subject, WhoId, WhatId From Task where Id =: myId];
Contact c;
Lead l;
if(t.What.Type == 'Contact'){
     c = [Select Id, Name , Email From Contact Where Id =: t.WhatId];
}else{
     l = [Select Id, Name , Email From Lead Where Id =: t.WhatId];
}

You could also simplify this easily creating a query string dynamically and using queryLocator, so you have only one query for the related object.

The main thing to take away here is that the WhoId and WhatId do not carry full access to the related object like other normally-related objects, so you need to use an additional SOQL query to get anything more than the Id, Type and Name for the related object.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .