+ Start a Discussion

Getting Custom Objects list without managed package objects.


Custom Objects (Unlimited Edition)  - 2,000
Note from Salesforce - (The custom objects contained in a managed package publicly posted on the AppExchange don't count against the limits for your Salesforce Edition).
//It will get list of all custom objects.
Map <String, Schema.SObjectType> allObjects = Schema.getGlobalDescribe();                
Map<String, Schema.DescribeSObjectResult> allObjectsNames = new Map<String, Schema.DescribeSObjectResult>();       
for(Schema.SObjectType objType : allObjects.values())        
                        Schema.DescribeSObjectResult objectResult = objType.getDescribe();                                    	        
                           if(objectResult.isCustom() && !objectResult.isCustomSetting() && !objectResult.getName().endsWith('__c_hd'))
                                allObjectsNames.put(objectResult.getName(), objectResult); 

I am writing the above code for getting all custom objects.

It is getting all custom objects including managed package objects. How can I avoid(filter) managed package objects.

I didn't get any reply from Apex Code Development, so again I raised this post in VF Development.
Can you please help any one.

There is no API exposed by Salesforce for DescribeSObjectResult class. But one workaround you can have is that use "getName()" method. If it is from some Package then it will have namespace as a prefix.

Thank you for reply! its really great thinking to avoid objects with namespace as prefix, but I have small doubt, if we install any additional package then one more name space will add to our sandbox. is it help full to get all packages name space dynamically and filter those objects in codding.

Clay Tomerlin 1Clay Tomerlin 1
I used a regular expression matching to filter out mine:
public List<SelectOption> getName(){ 
  List<SelectOption> options = new List<SelectOption>(); 
  //some ugly regular expression to identify any package prefix, 
  //will not match custom objects unless they use double underscore in the name 
  Pattern regEx = Pattern.compile('^\\w*?__\\B\\w*__'); 
 for(Schema.SObjectType item : ProcessInstance.TargetObjectId.getDescribe().getReferenceTo()) { 
    Matcher filter = regEx.matcher(item.getDescribe().getLocalName().toLowerCase());
    //use the Matcher class find() method to find the first instance of the match and return true or false.

     //Important to note: Don't send a debug message with .find() before this point, as each time its called it iterates to the next place it would be found and will force this to false
     if(!item.getDescribe().CustomSetting && (filter.find() == false)) //I like verbose boolean checks 
         options.add(new SelectOption(item.getDescribe().getLocalName().toLowerCase() , item.getDescribe().getLabel() )); 
return options;