You need to sign in to do that
Don't have an account?
Lakshman
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
Use this Code:
Hope this will help.
All Answers
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.
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
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.
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
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 ! :)
Haan yaar, Nice Thinking :)
Regards,
Lakshman
Use this Code:
Hope this will help.
Wow really, that sounds interesting. Didn't know such a thing existed ! Well I guess you learn something new everyday, eh !
Hello Bhawani,
Thanks for your exact Solution, Its working perfectly across all Environments, I tested it just now.
I appreciate your work.
Regards,
Lakshman
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
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\'');
Thanks for this and one more thing how i can store this result in a field?
Thanks in advance :)
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'));
}
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
Be careful -- key prefixes are case sensitive, == is not. That means there's a chance this could return unwanted results. Use this instead:
You can use the getSObjectType() method of Id class, to obtain an sObject token from an ID.