Is there a way to determine which object an Id belongs to?

  • 1
  • Question
  • Updated 4 years ago
Is there a way to determine which object an Id belongs to?
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
The object prefix is the first three characters of the object ID.  Take those three characters and create a URL like this: 

https://[instance].salesforce.com/[object prefix]/o<br>
You should get an overview page for the object. 
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Is there an object of these three characters anywhere?
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
You can... kindof.  The first three characters of an id are its keyprefix.  Skuid models have a property of keyprefix and objectName, so if you have a model on the page, you can figure out the object from an id.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Yeah, I don't know of a way to do this in Javascript land.
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
I was thinking of creating one gigantic object variable in order to do this in javascript using this list. I would only have to manage adding the custom objects in the org.

https://help.salesforce.com/apex/HTViewSolution?id=000005995&language=en_US

What do you think? As a javascript object I figure it would be practically instant to determine the object for the id using this method.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Yeah, that would work as long as you don't mind keeping up with that list.
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
It would only be in one that I would manage it. Static Resource javascript. Starting to really enjoy Sublime Text 3 w/ Mavensmate.
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Here's a simple example if you need something to get started with:

function EntityPrefixDecoder(recordID) {
    var prefixEntityMap = {
        '001': 'ACCOUNT',
            '002': 'NOTE',
            '003': 'CONTACT'
    };
    if (recordID === null || (recordID.length !== 15 && recordID.length !== 18)) {
        return null;
    }
    return prefixEntityMap[recordID.slice(0, 3)];
}
var id = '00290000000jPatPAT';
console.log(EntityPrefixDecoder(id));