function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AdmiralRontonAdmiralRonton 

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:

http://www.sforce.com/us/resources/tn-1.jsp

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

Thanks!
DevAngelDevAngel

Hi AdmiralRonton,

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

iperez_geniusiperez_genius
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...
?
VarunCVarunC
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)
Alias
FirstName
LastName
Name
Type
UserRole
UserRoleId
"

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 ?
vanessenvanessen

you can use prefixes like this:

 

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

 

 

for(Task t0 : trigger.new){
  if(((String)t0.WhoId).startsWith(case_prefix)){
  //do somerthing
  }
 }

 

 

enjoy it ....

 

VarunCVarunC

Thanks. Yes that would also work.

EMODULOR1EMODULOR1

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

SuperfellSuperfell

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

 

EMODULOR1EMODULOR1

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 );

 

if(task_whatid.startsWith(account_prefix)){

acousticacoustic

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

MandyKoolMandyKool

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.
IT SLATEIT SLATE

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