+ Start a Discussion
patrospatros 

Determine an object's type by id?

I'm working on a VF page that will determine the object type of an ID passed to it. The ID is a salesforce id which could be any object type (Account, Contact, etc).

 

I can't seem to get from here to there through Apex. I've check the describe calls but I can't find something that let's me create an object based on an ID and determine its type...

 

My only other option looks like the last resort of looping through ALL object types and trying a select query against each type.

 

Any ideas on this?

 

Below is the pseudocode of what I don't want to do. If I go this route, I'm going to hit the 100-SOQL-call limit very quickly. Feels like there must be a better way.

 

String objectID = getParameter('id');

String objectTypeName = '';

foreach(sObjectType type in types) {

sObject testObject = Database.query('SELECT Id FROM ' + type.Name + ' WHERE ID = "' + objectID + '"');

 

if(sObject.id != '') {

objectTypeName = type.Name;

break;

}

}

aalbertaalbert

Not sure if this helps, but here is a posting where I included an apex snippet that uses the describe calls in apex to store the key prefix in a map to get the object name.

 

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=12963#M12963

 

 

 

jawtechjawtech

thank you very much for the above posted apex code.  It's a shame that I had to use it though. Salesforce should provide more contextual/session values to determine what state the screen is in. For example, when I click a button, can the button refer to the current page/ object it is being used on? current URL? anything to let me redirect back to the page or provide more information to my apex page.