How to Fetch All Object API Names Using Dynamic Apex in Salesforce?

In the last article we learned about What is dynamic Apex and its use cases?. Now in this article we will learn how to fetch API names of all Object using Dynamic Apex in Salesforce. This is one of the use cases which we have listed.

Lets write the code and understand it.

First let’s create a map to store the sObject API Name and label.

Map<String, String> sObjectAPINameToLabelMap = new Map<String, String>();

Now We will use the Schema.getGlobalDescribe() method which will retrieve metadata of all objects available in the org. This method returns a Map where key is the API Name of the Objects (e.g., Account, Contact) and the value is the corresponding SObjectType which provides further details about the SObject.

Map<String, Schema.SObjectType> globalDescribeMap = Schema.getGlobalDescribe();

Now we will iterate on the globalDescribeMap which we created above. In line number 2 we retrieved the SObject using API Name. In line number 3 we are using the getDescribe method which will give the metadata of the SObject. In Line Number 4 we are inserting the Object API Name and Label in the map which we created earlier.

for(String globalDescribeKey : globalDescribeMap.keySet()) {
    Schema.SObjectType currentSObjectType = globalDescribeMap.get(globalDescribeKey);
    Schema.DescribeSObjectResult currentSObjectResult = currentSObjectType.getDescribe();
    sObjectAPINameToLabelMap.put(currentSObjectResult.getName(), currentSObjectResult.getLabel());
}

Complete Code

Here is the complete code with comments so that you can understand it better at the end we have also debug the complete map so that you can see what is stored in the sObjectAPINameToLabelMap.

//Map to store Object API Name and Label
Map<String, String> sObjectAPINameToLabelMap = new Map<String, String>();

// Map to Store the Metadata of all Objects 
Map<String, Schema.SObjectType> globalDescribeMap = Schema.getGlobalDescribe();

// Iterating on each object
for(String globalDescribeKey : globalDescribeMap.keySet()) {
    
    //Getting the sObject type
    Schema.SObjectType currentSObjectType = globalDescribeMap.get(globalDescribeKey);
    
    //Getting the sObject description result from sObject Type
    Schema.DescribeSObjectResult currentSObjectResult = currentSObjectType.getDescribe();
    
    // Getting the API name and Label of sObject and adding it to the map
    sObjectAPINameToLabelMap.put(currentSObjectResult.getName(), currentSObjectResult.getLabel());
}

for(String sObjectAPIName : sObjectAPINameToLabelMap.keySet()) {
    System.debug(sObjectAPIName + ' ==> ' + sObjectAPINameToLabelMap.get(sObjectAPIName));
}

Also have a look at what is stored in the sObjectAPINameToLabelMap.

Also, check out the video on How to fetch API Names of all Salesforce Object using Dynamic Apex?

We hope now you know how to fetch How to Fetch All Object API Names Using Dynamic Apex in Salesforce?. We will look at more use cases in future articles.

Leave a Comment