Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an Id and I want to know if it's possible to get the SObject with this unique ID ?

Something like :

SObject object = getSObjectById(myId);

I didn't find a method allowing that !

share|improve this question
up vote 7 down vote accepted

I'm not sure you could do exactly that, but what you could do is use the Id instance class to achieve what you need, for example:

Schema.SObjectType sobjectType = myId.getSObjectType();
String sobjectName = sobjectType.getDescribe().getName();

SObject record = Database.query('Select Id, Name From ' + sobjectName + ' Where Id = :myId');

See ID class documentation here for more details.

share|improve this answer
Yes, that's what I'm actually doing. But if there is no other way to do it, I will keep this code. Thank you for your help ! – SF_user Jul 29 '14 at 9:31
I don't believe there is another way of doing it, certainly not one I have come across. You could also use the describe information to get all the field names too giving you a more 'complete' object. – Phil Hawthorn Jul 29 '14 at 9:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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