In one of our article we have discussed how to fetch all custom objects in apex . Today we will learn how to get all Custom and Standard Objects .
You know that using Schema.getGlobalDescribe() we can fetch all properties of sObject. We have already used Schema class to fetch all Standard objects , record types from object , to find all required fields on Object and so on .
Get all Standard and Custom Objects
for(Schema.sObjectType info : Schema.getGlobalDescribe().values()) {
Schema.DescribeSObjectResult describedInfo = info.getDescribe();
String objectName = describedInfo.getLocalName();
System.debug('Object Name '+objectName );
}
The given approach will return all Objects available on Salesforce org . Where some objects are
The about code will give objects which are isAccessible and isQueryable and isCreateable .Check how to find all the Custom Objects in Salesforce.
Hope you learn something new Today .
Thank you.
How the code works
The loop goes through every object returned by Schema.getGlobalDescribe() and prints its local name. The second loop filters the list with isAccessible(), isQueryable() and isCreateable(), so you only see objects the running user can use.
Note about the title
Both snippets above return standard and custom objects. To get only standard objects, add a check for isCustom().
Only standard objects
List<String> standardObjects = new List<String>();
for (Schema.SObjectType info : Schema.getGlobalDescribe().values()) {
Schema.DescribeSObjectResult d = info.getDescribe();
if (!d.isCustom() && d.isAccessible() && d.isQueryable()) {
standardObjects.add(d.getName());
}
}
standardObjects.sort();
System.debug(standardObjects);
Describing every object is slow in a large org, so run this in the Developer Console, in a test, or cache the result. Do not run it for every record in a trigger.
FAQ
Why do some objects fail isCreateable? Many system objects such as history and share objects are read only.
How do I get only custom objects? Use d.isCustom() or check that the name ends with __c.