I have a scheduled Apex job that queries a child object and ordered by the parent Id. Then I have a for loop that groups all the child objects w/ the same parent Id into a list and then cache them in a map using the parent Id as a key. Before cache into a map, the key-value pair was correct, but then after if's put into a map, the value gets overwritten. Below is the Apex job. Thanks for all your help!!
global class LScheckforDefaultIntPayment implements Schedulable {
global void execute(SchedulableContext ctx) {
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime, State, CronJobDetailId
FROM CronTrigger WHERE Id = :ctx.getTriggerId()];
Map<Id, List<Loan_Servicing__c>> tsid_lsListMap = new Map<Id, List<Loan_Servicing__c>>();
List<Loan_Servicing__c> lsList = new List<Loan_Servicing__c>();
LSClass.loadRTMap();
List<Loan_Servicing__c> lsListTemp =
[SELECT Id, Name, Term_Sheet__c, Status__c
FROM Loan_Servicing__c
WHERE Amount__c > 0
AND Status__c != 'Invalidated'
AND Name LIKE 'TS -%'
AND (RecordTypeId = :LSClass.rtMap.get(LSClass.INT_ACCR_UNPAID) OR
RecordTypeId = :LSClass.rtMap.get(LSClass.INT_ACCR))
ORDER BY Term_Sheet__c];
System.debug('LScheckforDefaultIntPayment() >> lsListTemp='+lsListTemp);
ID ts_id = null;
tsid_lsListMap.clear();
for (Loan_Servicing__c ls : lsListTemp) {
if (ts_id != ls.Term_Sheet__c) {
if (ts_id != null) {
System.debug('ts_id='+ts_id+' lsList='+lsList);
tsid_lsListMap.put(ts_id, lsList);
System.debug(tsid_lsListMap.size()+' '+tsid_lsListMap);
lsList.clear();
}
ts_id = ls.Term_Sheet__c;
}
lsList.add(ls);
}
// System.debug('LScheckforDefaultIntPayment() >>> tsid_lsListMap size='+tsid_lsListMap.size()+''+ tsid_lsListMap);
// LSClass.checkforDefaultIntPayment(tsid_lsListMap);
}
}