Concepts or coding lessons of Salesforce that you can implement easily

The Best Ways to Retrieve Names of All Objects Using Apex

The Best Ways to retrieve a list of all Objects using Apex, you can use Schema.getGlobalDescribe() method.


Its simple use below code:

for( Schema.SObjectType o: Schema.getGlobalDescribe().values()){
    System.debug(o.getDescribe().getName());
}

Schema.getGlobalDescribe() :


It returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in your organization.

The map has the following characteristics:
  • Schema.getGlobalDescribe() method is dynamic, that is, based on permission, it is generated at runtime on the sObjects currently available for the organization. 
  • The sObject names are case insensitive.
  • The keys which are return by this method are prefixed with the namespace, if any.*
  • The keys indicate whether the sObject is a custom object or not. 
The cached Schema.SObjectType is my most favored approach because:
  • No SOQL queries
  • No describe calls
  • No function invocations.
If you want to exclude Share, history , Tag and feed objects then here is code
that will give the list of all objects excluding. 

You can add an extra filter condition to filter out other unnecessary sObjects.

global static List<String> getAllCustomSObjects()
{
    List<String> sObjectList = new List<String>();
    for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
    {
        Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe();
        if(describeSObjectResultObj.isCustom())
        {
            String name = objTyp.getDescribe().getName();
            // Exclude all the unwanted Sobjects e.g. History, Share etc.
            if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
             !name.containsignorecase('share') && !name.containsignorecase('feed'))
            {
                SobjectList.add(name);
            }
        }
    }
    system.debug('SObjectList****' + SObjectList);
    return sObjectList;
}

Note: Note: If managed package contains the getGlobalDescribe method and code related to managed package executed , then it returns sObject names and tokens for Chatter sObjects, such as UserProfileFeed & NewsFeed, even if Chatter is not enabled in the installing organization. But This is not true in normal class behavior that is, if the getGlobalDescribe method is executed from an apex class not within an installed  managed package.

Complete Example:


Step 1: Create Visualforce page

<apex:page controller="GetAllObjectsListController">
 <apex:form >
   <apex:pageBlock id="AllObjectsList">
    <apex:outputlabel value="Object Name"/>      
        <apex:selectList value="{!SelectedObject}" id="ObjPickList" size="1">
       <apex:selectOptions value="{!objList}"/>
    </apex:selectList>
 </apex:pageBlock>
 </apex:form>

</apex:page>

Step 2: Create Apex Controller

public class GetAllObjectsListController {
 Public string SelectedObject{get;set;}
 Public Map<String, Schema.SObjectType> AllObjectMap;
 Public GetAllObjectsListController(){
    AllObjectMap = New Map<String, Schema.SObjectType>();
    AllObjectMap = Schema.getGlobalDescribe();
}
Public List<selectoption> getAllObjList(){
    List<selectoption> SobjectList = new List<selectoption>();
    for(string s:AllObjectMap.keyset()){
        SobjectList.add(new selectoption(s,s));
    }
  return SobjectList;   
 }
}

There are other ways to retrieve sobject list from Salesforce, find below.
  • You can retrieve sobjects from API. From the API, you can see them via the Enterprise version of the WSDL or you can use the meta data API to access information about the customizations.
  • From Workbench also you can see the sobjects list.
Follow below steps:

1. Log in into your Salesforce Environment.
3. Select Salesforce Environment, API version and Accept the Terms and conditions. 
4. Go to utilities Tab
5. Select REST Explorer.
6. Click on Execute button. (Make sure HTTP Get method is selected)
7. Click on /services/data/v37.0/sobjects
8. You can expand all to see the objects list.

Enjoy! If you have any questions, comments, please feel free to let me know. 
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.
loading...

No comments:

Post a Comment