Recently I came across a question on developer board to get Salesforce Id for a Custom Object. As we all know, Currently its not available in metadata and describe API, therefore either we have to store these Ids in Custom Setting or to put some hard code logic. I wrote an apex code to find the Object Id so that we can avoid hard coding ids.
The code uses, standard Custom Object url and parse the URL to retrive the Object Name and its Salesforce Id.
Here is the Apex Class:
And the example code to retrieve Custom Object Id
If needed, just copy the above class and use the method shown in example to get the Id. I did not created test class for this, so please create by yourself.
In case of any queries/issues with the above code, do let me know.
Cheers!
Prafull G
The code uses, standard Custom Object url and parse the URL to retrive the Object Name and its Salesforce Id.
Here is the Apex Class:
/* Class to find Salesforce Id for a specific custom object */ public class DescribeCustomObject { // Pattern to be searched private static final Pattern OBJECT_PATTERN = Pattern.compile('<a href="/(\\w*)\\?setupid=CustomObjects">(.+?)</a>'); private static final String CUSTOM_OBJECT_PAGE_URL = '/p/setup/custent/CustomObjectsPage?setupid=CustomObjects'; // Method to get the Custom Object Id public static String getCustomObjectId(String custObjName) { // Check the parameter if(custObjName == null || custObjName == '') { return null; } // find out all listed custom object Ids map<String, String> mapCustObjectIds = findObjectIds(); String customObjectId = mapCustObjectIds.get(custObjName); // Find custom object Id return customObjectId; } // Method to get the content and prepare map of Custom object with its Salesforce Id private static map<String, String> findObjectIds() { // PageReference instance. NOTE: the URL is standard and does not supposed to be change between different orgs Pagereference pr = new PageReference(CUSTOM_OBJECT_PAGE_URL); // Get the Page content and store as String String htmlContent = pr.getContent().toString(); // Matcher for the defined pattern Matcher match = OBJECT_PATTERN.matcher(htmlContent); // Map to store Object Name with its Salesforce ID map<String, String> mapObjectIds = new map<String, String>(); // Iterate the matcher and find out the specified pattern while(match.find()) { // If matched, Add the custom object and Id to the map mapObjectIds.put(match.group(2), match.group(1)); } // Return map of Object Ids return mapObjectIds; } }
And the example code to retrieve Custom Object Id
// Pass the Custom Object Label to the method. I passed 'Candidate' String customObjectId = DescribeCustomObject.getCustomObjectId('Candidate'); // Print the Id retrieved System.debug('Candidate Object Id====' + customObjectId);
If needed, just copy the above class and use the method shown in example to get the Id. I did not created test class for this, so please create by yourself.
In case of any queries/issues with the above code, do let me know.
Cheers!
Prafull G