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
SachiiSachii 

How to get sObject type from the Id?

How to get sObject type from the Id?

 

I dont want to use the SOSL query approach or the exception handling approach. Is there any other straight forward way  I can get the API name of the object if i pass the Id?

 

I attempted something like below

 

If(Account.sObjectType==new sObject (sId).getsObjectType())

 

expecting new sObject (sId).getsObjectType() to return Account.sObjectType if 'sId'  belongs to an accoun record.

 

Please help and share your experience when you encountered similar situation.

 

 

~Sachi

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

If you use Schema.getGlobalDescribe(), you may iterate through the returned objects to find the object which has the keyPrefix that matches the leftmost three characters of the ID. If you need this repeatedly, you can use a map<string,string> to map the keyPrefix to the API name, like this:

 

Map<String,String> keys = new Map<String,String>();
Map<String,Schema.SobjectType> describe = Schema.getGlobalDescribe();
for(String s:describe.keyset())
keys.put(describe.get(s).getDescribe().getKeyPrefix(),s);

Once you have keys populated, you can then determine a record's type using keys.get(String.valueOf(recordId).substring(0,3)). This is technically the long way around, but there isn't any way to simply ask "GetObjectTypeById(recordId)".

All Answers

SachiiSachii

Poor I am. I just found out SOSL can have "Id" in fields group. Any other approach?

sfdcfoxsfdcfox

If you use Schema.getGlobalDescribe(), you may iterate through the returned objects to find the object which has the keyPrefix that matches the leftmost three characters of the ID. If you need this repeatedly, you can use a map<string,string> to map the keyPrefix to the API name, like this:

 

Map<String,String> keys = new Map<String,String>();
Map<String,Schema.SobjectType> describe = Schema.getGlobalDescribe();
for(String s:describe.keyset())
keys.put(describe.get(s).getDescribe().getKeyPrefix(),s);

Once you have keys populated, you can then determine a record's type using keys.get(String.valueOf(recordId).substring(0,3)). This is technically the long way around, but there isn't any way to simply ask "GetObjectTypeById(recordId)".

This was selected as the best answer
SachiiSachii

Thanks fox..this really helps and a much better way. am making this as a global utility method. But I will also have to confirm the heap size and execution statements. Thank you very much.

Brian Miller 37Brian Miller 37
If variable sId is a valid primitive Id type, then this should do the trick:
String sobjectType = sId.getSObjectType().getDescribe().getName();
Parag Bhatt 10Parag Bhatt 10
Just try this code if you want to retrive the object name from a string ..
public string selectedobject{get;set;}
 public sObject sObj{get;set;}
Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
 Schema.SObjectType s = m.get(selectedobject) ;
   sObj =s.newSObject();
Like this answer if it helps you...

 
Gil GrenchoGil Grencho
@Brian Miller 37, is the best and simpler answer.
Nikita ChizhikNikita Chizhik
Id idToProccess = '00611000002ZXX';              
Schema.sObjectType entityType = idToProccess.getSObjectType();
System.assert(entityType == Opportunity.sObjectType);

source: https://devforce.co/tip-apex-get-sobject-type-from-id/  (https://devforce.co/tip-apex-get-sobject-type-from-id/ )