Simon Fell > Its just code > who.type

Thursday, June 5, 2008

Quick API tip, most of the API requires you to know both the ID of a record, and its type (e.g to update it, or to retrieve its field values). In most cases it pretty easy to get that, but where most people get tripped up is on polymorphic foreign keys (e.g. whoId on task), the trick is to remember the thing at the end of the relationship can tell you its type, so instead of using this query, then being left trying to work out what type the whoId is,

select id, whoId from task
instead use SOQL-R, and use this query, now you will know what type the whoId is without any further shenanigans (like calling describeSObject on everything and matching the keyprefix which is horribly slow way to do it)
select id,, who.Type from task
The follow on is that it then becomes easy to run queries that find all tasks ascociated with contacts, it's just
select id,, who.Type from task where who.Type='Contact'