Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

1 Answer 1

up vote 2 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 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 at 9:33

Your Answer

 
discard

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.