0

I have a custom object and it's history is shown in UI. I am tracking the certain field. However, I have a problem putting the parentId and list of its history records into the map.

Custom object : TestObject__c

History (standard) : TestObject_History

I need to create a Map that contains the Id of the TestObject__c and its list of TestObject_History in this format:

Map<Id, List<TestObject_History>> parentHistories = new Map<Id, List<TestObject_History>>();

I can create a nested for loop like this:

for(TestObject__c ob : arrayList) {
    for(TestObject_History his : [SELECT ParentId, NewValue FROM TestObject_History WHERE ParentId =: ob.Id]) {
        // statement
    }
}

However, I need to avoid writing the soql query inside the loop. Any other ideas? I would appreciate it.

1
  • I tried this one but doesn't seem to work: Map<Id, TestObject_History> mp = new Map<Id, TestObject_History>(); for(TestObject__c ObId : [SELECT Id FROM TestObject__c ]) { mp.put(ObId.Id, null); } mp.putAll([SELECT Id, Field, DataType, OldValue, NewValue, CreatedDate, ParentId FROM TestObject_History]); System.debug('mp ' + mp); System.debug('mp ' + mp.size()); Commented Sep 13, 2023 at 5:31

0

Browse other questions tagged .