Tuesday, March 19, 2013

Apex Code: Get Custom Object Id

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:
/*
 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

4 comments:

  1. Salesforce Admin Online Training - 21st Century Software ...
    www.21cssindia.com/courses/salesforce-admin-online-training-143.html
    Salesforce admin online training by 21cssindia the largest institute in providing online trainings in all technologies. Salesforce admin training, salesforce ...
    salesforce developer online training| salesforce developer ...
    www.21cssindia.com/.../salesforce-developer-online-training-144.html
    21cssindia provides Salesforce developer online training by real time Experts. Call us +91 9000444287 for online training and demo. Online Salesforce ...
    salesforce crm online training| salesforce crm training| call ...
    www.21cssindia.com/courses/salesforce-crm-online-training-212.html
    21cssindia provides Salesforce crm online training by real time Experts. Call us +91 9000444287 for online training and demo. Online Salesforce crm training ...

    ReplyDelete
  2. Do you have the test class for this class ?

    ReplyDelete
    Replies
    1. Hi Ayush,

      I do not have one handy. But the code I used to run the code i.e. "example code to retrieve Custom Object Id" can be used within test method to get coverage.

      Thanks,
      Prafull

      Delete