You need to sign in to do that
Don't have an account?

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!
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!
Hi AdmiralRonton,
The only option available at this time is the one presented in the tech note.
if i am looking to find out if a task belongs to a lead or a contact how exactly to i code that...
?
"[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 ?
you can use prefixes like this:
String contact_prefix = Schema.SObjectType.Contact.getKeyPrefix();
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.
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)){
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
}
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 ?