+ Start a Discussion

How to get the Related To field value of activity in soql

How to get the Related To field value of activity in soql(this field have lot of objects including custom objects)?


Data Type Lookup(Contract,Campaign,Account,Opportunity,Product,Asset,Case,Solution,Asset Summary,Investor List, etc)

Investor List  is custom object.

In above data type I need to get Investor List value in soql? how to get this value in soql?


Hi JamesSS,


Task.whatId will give you the id of the Related To record and Task.What will let you access some limited fields for the Related To record like Name.  If you want more fields then you will need to get the sObjectType of the ID and you can then create a SOQL query using the sObjectType and whatId.


Task t = [select id, whatId,what.Name from Task limit 1][0];

string relatedToType= t.whatId.getSObjectType().getDescribe().getName();

if (relatedToType=='Case'){

Case c =[select id,name,field1,field2,field2 from Case where id=:t.whatId];

else if (relatedToType=='Custom__c'){

Custom__c custom = [select id,name,field4,field5,field6 from Custom__c where id=:whatId];



sObject relatedTo=t.What;



this is a basic example and I haven't tested that it compiles but you get the idea and you could make this more dynamic.