0

My requirement as part of org analysis is to get list of all custom objects having more than 100000 records and created date of LAST CREATED record.

My tries are as follows:-

  1. Go to Setup - Storage usage - list of all objects with record count is available here.(however it is a mix of standard and custom objects moreover there is no way to get a last created date.

  2. BASED on

A. How can I know number of records of All Salesforce Object? B. Salesforce Objects & record Counts

I wrote following code(but it is giving fatal error possibly because of too many objects and 'change' condition processing time) Also without change condition it continues to give me errors:-

 for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )
 {        

    Schema.DescribeSObjectResult objResult = o.getDescribe();
    system.debug( 'Sobject API Name: ' + objResult.getName() );

    String obj = objResult.getName();
    if('EmailStatus' == obj || obj == 'Name' || obj == 'Vote' || obj == 'AggregateResult'
      || obj == 'CampaignChangeEvent' || obj == 'AccountChangeEvent' || obj == 'ContractChangeEvent'
       || obj == 'LeadChangeEvent' || obj == 'OpportunityChangeEvent' || obj == 
'OpportunityContactRoleChangeEvent' 
       ||  obj == 'Product2ChangeEvent' || obj == 'AssetChangeEvent' || obj == 'CaseChangeEvent' || 
 obj.contains('Change') )
        continue;

    String q = 'SELECT Count() FROM ' + obj;

    Integer count = Database.countQuery(q);

    System.Debug(objResult.getName() + '-->' + count);

}
0

You need count only for custom objects, so it is reasonable to add: if (!objResult.isCustom()) continue;

Overall, your code should look somehow like this:

for (Schema.SObjectType objectType : Schema.getGlobalDescribe().values()) {
    Schema.DescribeSObjectResult objectDescription = objectType.getDescribe();
    if (!objectDescription.isCustom()) continue;
    String objectName = objectDescription.getName();

    String query = 'SELECT Count() FROM ' + objectName;
    Integer count = Database.countQuery(query);

    if (count > 100000) {
        query = 'SELECT CreatedDate FROM ' + objectName + ' ORDER BY CreatedDate LIMIT 1';
        List<sObject> objects = Database.query(query);
        system.debug(objectName + '-> ' + count + ' -> ' + objects[0].get('CreatedDate'));
    }
}
| improve this answer | |
  • before printing system.debug check the size of the the objects. if(objects.size()>0){ system.debug('----');} – Annappa PH Mar 24 at 12:18
  • Thanks @annappa and Mariia. Code works but I am looking for a solution since there are more than 900 rows here, I am getting SOQL 101 error even after size check – Mayank Parnami Mar 24 at 12:57
  • @AnnappaPH you shouldn't check it because there are at least 100001 rows possible for that query, so you always can get [0] element. – Mariia Illarionova Mar 24 at 13:34
  • @MayankParnami You can execute this code in portions of 50 objects with offset. Just remember the last processed custom object in loop and process next iteration starting from it. – Mariia Illarionova Mar 24 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.