Query EntityDefinition


Recently I have encountered a scenario where I need to list down all the sObjects(custom or standard) which the businesses are currently using. They gave a hint to list down all objects for which at least a single record have been created on or after Jan 1,2025.
Initially I thought that I need to query each object once to find the list of results.
Nah!!! I had almost over 500+ objects will take me a week to prepare the list and it would be quite boring.
Then I found the sObject - EntityDefinition. - Here's the Salesforce official link
Now I need to construct a query with the help of which I can just list down all sObjects in the Org!!!
SELECT Id, Label, QualifiedApiName from EntityDefinition where IsQueryable = true and IsDeprecatedAndHidden = false and IsCustomSetting = false and IsCustomizable = true
Filters Used in the query
IsQueryable = true : the sObject can be queried and records will be returned(if any).
IsDeprecatedAndHidden : If true, this object is unavailable for the current version.
IsCustomSetting : If true, the object is a custom setting, set to false to ignore all custom settings.
IsCustomizable : If true, custom fields can be defined for the object. It is important to include this filter so that we can remove objects Feed tracking and History.
Once you run the query you will see all sObjects like below screenshot.
We’ve only just scratched the surface…Now the real pain lies behind querying each sObject to find if any records for each sObject have been created on or after Jan 1,2025, which we are not going to do one at a time definitely.
So let’s write a simple Apex code to have the count
Before that just convert your List of sObject like below
'PromotionTier', |
'WebStoreInventorySource', |
'AppointmentCategory', |
'PromotionLineItemRule', |
'Waitlist', |
'WaitlistParticipant', |
'WaitlistServiceResource', |
'WaitlistWorkType', |
'PrivacyRTBFRequest', |
'ServiceAppointmentAttendee', |
'ApprovalSubmission', |
'ApprovalWorkItem', |
'DeliveryEstimationSetup', |
'LocationShippingCarrierMethod', |
'ShippingCarrier', |
'ShippingCarrierMethod', |
'ApprovalSubmissionDetail', |
'DataMaskCustomValueLibrary', |
'Location_Tracing__c', |
'Location__c', |
'People_Tracing__c', |
'Person__c', |
'Question__c', |
Here’s the apex Code -
Few tips to remember :
Always put 100 or lesser objects inside your List of allObjects so that you never hit "System.LimitException: Too many SOQL queries: 101" error.
We are not going to give debug logs to the business, So prepare an excel manually or create a custom object to populate the details and later on extract and share
List<String> allObjects = new List<String>{
'Account',
'Contact',
'Opportunity',
'Case',
'Lead',
'PromotionTier',
'WebStoreInventorySource',
'AppointmentCategory',
'PromotionLineItemRule',
'Waitlist',
'WaitlistParticipant',
'WaitlistServiceResource',
'WaitlistWorkType',
'PrivacyRTBFRequest',
'ServiceAppointmentAttendee',
'ApprovalSubmission',
'ApprovalWorkItem',
'DeliveryEstimationSetup',
'LocationShippingCarrierMethod',
'ShippingCarrier',
'ShippingCarrierMethod',
'ApprovalSubmissionDetail',
'DataMaskCustomValueLibrary',
'Location_Tracing__c',
'Location__c',
'People_Tracing__c',
'Person__c',
'Question__c'
};
Map<String,String> objMap = new Map<string,String>();
//Below two variables are just to check that how many sObjects are we able to query successfully and how many has failed
Integer failedToQuery = 0;
Integer successfullQuery = 0;
for(String objName : allObjects)
{
try
{
Schema.DescribeSobjectResult d = Schema.getGlobalDescribe().get(objName).getDescribe();
String label = d.getLabel();
String query = 'SELECT count() FROM '+objName+' WHERE CreatedDate >= 2025-01-01T00:00:00Z';
Integer count = (Integer)Database.countQuery(query);
String objInfo = objName+' || '+label+' || '+count;
System.debug('Active Object Found with details '+objInfo);
objMap.put(objName,objInfo);//You can further utilize the map to send an email to admin or populate any custom object which will hold the details and count of each object.
successfullQuery++;
}
catch(Exception e)
{
failedToQuery++;
System.debug('Error Occured while querying object '+objName+' Error Message '+e.getMessage());
}
}
Subscribe to my newsletter
Read articles from SHARIQUE KALAM directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

SHARIQUE KALAM
SHARIQUE KALAM
I am a Salesforce developer with strong intention to learn full stack Salesforce Development and I am here to share my learnings and also to learn.