I know that Standard Objects are consistent in the prefixes for their record Ids, (001=Account, 002=Contact, 500=Case, 006=Opportunity, etc.).

I've created a custom object, and the record Ids for this object all start with a02. My question... if I package up this app and install it elsewhere (sell it through App Exchange), will the records maintain that same prefix?

| improve this question | | | | |
  • 2
    Just curious why you want to know? You can dynamically get the key prefix from the describe. – Adrian Larson Feb 13 '18 at 0:01
  • I'd expect not (my org is up to a1X or so), but I'm not an ISV, and haven't run across any supporting documentation...so I'll leave this for someone else to answer. – Derek F Feb 13 '18 at 0:13
  • I mean what are you trying to do with the prefix? Regardless of the answer here you shouldn't hard-code it. – Adrian Larson Feb 13 '18 at 0:17
  • I've created a Lightning Component and want it to be context aware of whether it's been dropped into an Account page, or whatever, based on recordId. It will display related records (of my custom object type), but the SOQL will vary based on what kind of page it is. – PatMcClellan__c Feb 13 '18 at 0:20

For identifying any object with a fixed key prefix (even custom objects), compare the SObjectType from the Id via Id.valueOf, or simply by casting to Id.

if(Id.valueOf(recordId).getSObjectType() == CustomObject__c.SObjectType) {

if(((Id)recordId).getSObjectType() == CustomObject__c.SObjectType) {

If it's already an ID, even better:

if(recordId.getSObjectType() == CustomObject__c.SObjectType) {

Keep in mind that if you assign a String to an Id, you get your cast for free; ideally, you should define recordId as an Id type:

Id recordId = ApexPages.currentPage().getParameters().get('recordId');

Whenever you're trying to use an Id, make sure it's in an Id type variable. It will allow you to write cleaner code and avoid subtle errors.

Specifically related to Lightning Components: your client side component logic can identify the type of object it's attached to without any server-side code at all. To do this, simply use the force:hasSObjectName interface:

<apex:component implements="force:hasSObjectName,force:hasRecordId">

This gives you an automatic attribute called sObjectName that you can use in later API calls, initialization code, etc, without the latency of calling the server at all.

| improve this answer | | | | |

Thanks for your advice @Adrian Larson. I've recoded all of them like this:

if(recordId.left(3)== Schema.SObjectType.Account.getKeyPrefix()){
    //more code here
| improve this answer | | | | |
  • Do not be tempted to do this. It is not the best possible approach. – sfdcfox Feb 13 '18 at 2:26

In Apex, the Id class has a method .getSObjectType() for this. It returns a Schema.SObjectType. From there you can make all the further describe calls that you want to.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.