I want to be able to reliably take an ID and map it to the appropriate object type so I can query the object in question.

Before now I've tried using the global decsribe info, looping over the objects and comparing the value of getKeyPrefix() witht the test value, but the problem is we're limited to 100 describe calls in the governor limits, and so this method breaks down in orgs with more than 100 objects.

The next best solution I can think of is to slowly build up map of prefix -> object inside a list custom setting, so if I miss an object first time around the second time I might get it and so on as each successive run will have a larger cache to test first. This doesn't seem ideal though, so I'm looking for other options.

Another might be to hard code the known prefixes 006 for Oppties etc. in a static apex map, but that feels dirty.

  • 2
    +1 to ca_peterson's experience - getSObjectType() does not appear IME to be restricted to 100 calls. Also, this may be a red herring because the code is a bit old, but I have some record-to-key-prefix-mapping code that has hardcoded returns for a few objects: CampaignMember, Task, User, and EmailTemplate, with a code comment that these aren't visible in global describes inside managed packages. Which seems weird, but I wrote the comment so I am pretty sure this was the case at some point, so if you're in a MP just watch out for that if relevant.
    – jkraybill
    Commented May 2, 2013 at 5:25

2 Answers 2


Despite what the docs would seem to indicate you are allowed to call getDescribe() on over 100 SObjectType objects in order to get their key prefix (the limits seem to apply to enumerating fields and global describes).

I have an answer on a similar question with details on how to implement your idea, but there is a much cleaner way of doing this. To quote from the answer:

In APIv26 there's a new method, Id.getSObjectType() which returns a sObject token you can use to find it's type. Sample code:

String objectAPIName = someId.getSObjectType().getDescribe().getName();

This does not appear to be limited to 100 calls either, so you should be able to use this on very large numbers of IDs.

  • 1
    Son of a.... wish I'd known about that method sooner!
    – Matt Lacey
    Commented May 2, 2013 at 6:12

If you only have the key prefix you can pad it out with the empty id string:

String emptyId = '000000000000AAA';
String prefix = '00D';
ID myId = prefix + emptyId;



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .