Reply
Visitor
AdmiralRonton
Posts: 2

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!
Moderator
DevAngel
Posts: 3,214

Re: Getting Object type of WhatId/WhoId Task/Event fields

Hi AdmiralRonton,

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

Dave
Developer Evangelism
BLOG - blog.sforce.com

Follow me on twitter @dcarroll
Regular Contributor
iperez_genius
Posts: 32

Re: Getting Object type of WhatId/WhoId Task/Event fields

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...
?
Trusted Contributor
vchaddha
Posts: 455

Re: Getting Object type of WhatId/WhoId Task/Event fields

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 ?
-------------------------------
blog | ideas
Regular Contributor
vanessen
Posts: 78

Re: Getting Object type of WhatId/WhoId Task/Event fields

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 ....

 

Trusted Contributor
vchaddha
Posts: 455

Re: Getting Object type of WhatId/WhoId Task/Event fields

Thanks. Yes that would also work.

-------------------------------
blog | ideas
Contributor
EMODULOR1
Posts: 3

Re: Getting Object type of WhatId/WhoId Task/Event fields

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

Moderator
SimonF
Posts: 7,676

Re: Getting Object type of WhatId/WhoId Task/Event fields

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

 

Cheers
Simon
docs | blog | twitter
Contributor
EMODULOR1
Posts: 3

Re: Getting Object type of WhatId/WhoId Task/Event fields

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