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

how to Identify/detect Object Name from ID on visualforce page via Apex code?

please suggest how can we Identify/detect Object Name from ID on visualforce page via Apex code?


i am trying to dynamically pick the name of standard/custom object from salesforce object id on a visualforce page.


please suggest if you have any ideas on that.



Here's some sample code you can adapt:


The output is the label of the Sobject, but you can pretty easily change that to return the API name, and from that you can instantiate a new Sobject as needed.


Good luck!


Hi John,

thanks a lot for your help on this.




Here's another approach wherein you use a static class that can be shared by many controllers/triggers:


public class MySingleton {
public  static Map<String, Schema.SObjectType> 		sobjToSobjDescribeMap			= null;

public static Boolean idIsOfSObj(String SObjName, ID theId) {
		if (sobjToSobjDescribeMap == null) 
			sobjToSobjDescribeMap= Schema.getGlobalDescribe();
		String idKeyPrefix = theId != null ? ((String) theId).substring(0,3) : null;
		if (sobjToSobjDescribeMap.get(SObjName).getDescribe().getKeyPrefix() == idKeyPrefix) return true;
		return false;	

// invoke this from anywhere by using if (MySingleton.idIsOfSObj(someIdVbl,'Opportunity')) { /* true logic }

 This way you avoid the Governor limit on Describe calls