function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LakshmanLakshman 

How to get the object API name by Id?

Hi All,

I want to get the name (API) of object (Custom or Standard) by Id. Is this possible, is there any method for it.

 

Please help me regarding this.

Thank you!

 

Regards,

Lakshman

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

Use this Code:

 

 

String accId = '00190000005gJcl'; // replace this id with your one

String keyCode  = accId.subString(0,3);

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
for(Schema.SObjectType objectInstance : gd.values())
{
if(objectInstance.getDescribe().getKeyPrefix() == keyCode)
{//do your processing with the API name what you want
System.debug('Id is related to object: '+ objectInstance.getDescribe().getName());
}
}

 

Hope this will help.

 

 

 

All Answers

Ritesh AswaneyRitesh Aswaney

There is no API method  to get this, but maybe you could set up a custom setting to map these - the first three digits of the id indicate the object, eg 001 is Accounts, 003 is Contacts, etc. 

You can get hold of these quickly by clicking on the Object tabs - and you could grab them off the URL.

 

LakshmanLakshman

Hi Ritesh,

I completely agree with your solution and currently I have done the same. But the custom object id will change in production, then what will we do?

In sugar CRM there is an API (which internally uses the same logic of checking the first three digits of id and comparing with the standard one) for this but why in salesforce there is not an equivalent API for this.

 

Regards,

Lakshman

Ritesh AswaneyRitesh Aswaney

Hey Lakshman,

Agreed the IDs will change across environments, however I doubt if the prefixes (first 3 digits) change. I've worked across a few orgs, and if memory serves me right, Accounts always start with 001 in any org.

 

Another point to note is that if you have refreshed your dev sandboxes from Production, then most of the IDs are in synch with Prod, and remain unchanged.

LakshmanLakshman

Hi Ritesh,

You are absolutely right, standard object id won't change. I hope that custom object id should also not change, what do you say?.

BTW Thank you for your opinion.

 

Regards,

Lakshman

Ritesh AswaneyRitesh Aswaney

Just checked ID's for a couple of custom objects - same across sandbox and prod, but this one may one have been refreshed from prod. if that is the case, you're always safe.

 

Anyway, if its a Custom Setting, then you're already reducing the impact to a minimum, as they are so configuration-friendly.

 

Suppose you could always write an Apex class to auto-populate the custom settings with environment specific values - but I wouldn't deem that necessary. more the sort of stuff you'd take up when you're bored on a saturday afternoon, with nothing else to do ! :)

LakshmanLakshman

Haan yaar, Nice Thinking :)

 

Regards,

Lakshman

Bhawani SharmaBhawani Sharma

Use this Code:

 

 

String accId = '00190000005gJcl'; // replace this id with your one

String keyCode  = accId.subString(0,3);

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
for(Schema.SObjectType objectInstance : gd.values())
{
if(objectInstance.getDescribe().getKeyPrefix() == keyCode)
{//do your processing with the API name what you want
System.debug('Id is related to object: '+ objectInstance.getDescribe().getName());
}
}

 

Hope this will help.

 

 

 

This was selected as the best answer
Ritesh AswaneyRitesh Aswaney

Wow really, that sounds interesting. Didn't know such a thing existed ! Well I guess you learn something new everyday, eh !

LakshmanLakshman

Hello Bhawani,

Thanks for your exact Solution, Its working perfectly across all Environments, I tested it just now.

I appreciate your work.

 

Regards,

Lakshman

HoneyHoney

Hi,

How i can write query on this object now? for e.g. i need to take some field from this object, then how i can write [selct id from ------------];

please suggest some... Thanks

Bhawani SharmaBhawani Sharma

You can create dynamic query to get the result from this object. like 

 

List<SObject> records = Database.query('Select Id from ' + objectName + ' where Name = \'something\'');

HoneyHoney

Thanks for this and one more thing how i can store this result in a field?

Thanks in advance :)

Bhawani SharmaBhawani Sharma

Result is already stored in "records" variable.

 

if you want to process the results then:

for(SObject sObj : records)  {

System.debug('Id field value:::::' + sObj.get('id'));

}

 

 

SAKTHIVEL MSAKTHIVEL M

HI,

You can find the Salesforce Standard Objects Prefixed Ids Using below link,

http://theblogreaders.com/list-of-salesforce-standard-objects-prefixed-ids


TheBlogreaders.com
Blog | Salesforce Certified Administrator | Salesforce Certified Developer

 

 

Danny SalvadoriDanny Salvadori
if(objectInstance.getDescribe().getKeyPrefix() == keyCode

Be careful -- key prefixes are case sensitive, == is not. That means there's a chance this could return unwanted results. Use this instead:
if (keyCode.equals(objectInstance.getDescribe().getKeyPrefix())) {
AzeezAzeez

You can use the getSObjectType() method of Id class, to obtain an sObject token from an ID.

Id myId = '0016F00002FPK2d';
System.debug('sobject type is '+ myId.getSObjectType());
System.debug('sobject name is '+ myId.getSObjectType().getDescribe().getName());
/*to check if the Id of a particular sObject Type, use below line*/
System.debug(myId.getSObjectType() == Schema.Account.SObjectType);