I need to retrieve the CreatedDate of the last record creates of every custom object. But the only way I find to do it is by doing a dynamic query in a For or loop. But I’m, of course, limited to 100 custom objects. Did anyone have a solution to avoid the query in the loop ? Or any other workaround ? Thanks
I give you a sample of my code :
public void method() {
List<ObjectBuilder__c> listOfRecord = new List<ObjectBuilder__c> () ;
for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
{
ObjectBuilder__c oneRecord = new ObjectBuilder__c () ;
Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe();
objectName = objTyp.getDescribe().getLabel() ;
if(describeSObjectResultObj.isCustom() ) {
if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
!name.containsignorecase('share') && !name.containsignorecase('feed') ) {
oneRecord.Api_Name__c = objTyp.getDescribe().getName(); ;
List<sObject> tmp = Database.query('SELECT CreatedDate FROM '+ oneRecord.Api_Name__c +' order by CreatedDate desc limit 1 ');
System.debug('tmp list '+tmp);
for(sObject obj :tmp ) {
oneRecord.Last_Used_Date__c =Date.valueOf(obj.get('CreatedDate')) ;
listOfRecord.add(oneRecord) ;
}
}
}
}
INSERT listOfRecord ;
} `