+ Start a Discussion

Getting Object type of WhatId/WhoId Task/Event fields

I'm looking to find the object types of the WhatId/WhoId fields of the Task and Event objects, so I can query these objects and get more information about them and perform certain automated tasks based on the type.

I found this tech article, which uses the first 3 digits as a marker, but the article also says that this may change at any time:


Is there a better way of getting the object types of WhatId/WhoId fields or querying them based on Id only (no type)?


Hi AdmiralRonton,

The only option available at this time is the one presented in the tech note.

can i ask...
if i am looking to find out if a task belongs to a lead or a contact how exactly to i code that...
Well i found something a User poited out on ideas, HE SAID:

"[Select Who.name from task where id = :taskid];

Polymorphic relationships like who and what on task/event or owner on queue-ownable objects can be traversed. The object that is returned is called "Name". Here's the information you can access through this traversal:

(from the enterprise WSDL)

But i could not understand how I could read these columns in APEX class?
My query is like this:
Select Subject,WhoId,WhatId,Who.FirstName,Who.Name,Who.type,What.type From task

Now i get a Collection NAME here which contains (FirstName,Name,Type), but how I could read these Values in APEX class? Any idea ?

you can use prefixes like this:


String contact_prefix = Schema.SObjectType.Contact.getKeyPrefix();



for(Task t0 : trigger.new){
  //do somerthing



enjoy it ....



Thanks. Yes that would also work.


what tech note are you referring to?  the URL in the previous post is dead... please let us know!!!


The best way is to query for the type when you query for the Id, e.g.


select id, subject, who.id, who.type from Task



oh thanks, i ended up doing what another user posted and it worked!



if(ta.WhatId != null){

String account_prefix = Schema.SObjectType.Account.getKeyPrefix();

String task_whatid = ta.WhatId;

System.debug('who: ' + ta.WhoId + ' what: ' + ta.WhatId + ' prefix: ' + account_prefix + ' eval: ' + task_whatid.startsWith(account_prefix) + ' id: ' + ta.id );




vanessen, thank you so much! That little peace of code is golden!! :)


I know this post is really old.
But just in case if someone is looking to find the sObject associated with polymorphic fields, You can use getSobjectType() method associated wtih ID's to find out the type of sObject. This is more effieicent than describing the object and compairing key prefixes (Not sure if this was present way back in 2004 when this question was posted:) ).

if(recEvent.WhoId.getSObjectType() == Contact.sObjectType){
//Do something

Luke J FreelandLuke J Freeland
Despite this being old, you can use SOQL to grab activity records that are associated with a given object using the object's key prefix. For example, let's say you wanted to grab the tasks associated with a custom object. You can do that by specifying that the WhatId is between the min and max Id values for that given object like so:
String objectPrefix = Schema.SObjectType.<object_api_name>.getKeyPrefix();
String minObjectId = objectPrefix + '000000000000';
String maxObjectId = objectPrefix + 'zzzzzzzzzzzz';

List<Task> objectTasks =
[SELECT Id, WhatId
   FROM Task
  WHERE WhatId >= :minObjectId
    AND WhatId <= :maxObjectId];
Using this approach, all the heavy lifting is done at the database level and one doesn't have to waste Apex CPU time doing the filtering.

Unfortunately, the "like" operator isn't supported on Id fields.

write SOQL query to display Account,Task,Opportunity,case,event records in VF pages. HOW ?