1

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);
    }   
}
1

I think you can build your map to use containsKey that will be easy. Please check my code.

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 Term_Sheet__c != null  //filter it in the SOQL
             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];

        for (Loan_Servicing__c ls : lsListTemp) {
            Id ts_id = ls.Term_Sheet__c;

            if (!tsid_lsListMap.containsKey(ts_id)) {
                tsid_lsListMap.put(ts_id, new List<Term_Sheet__c>());
            }
            tsid_lsListMap.get(ts_id).add(ls);
        }
        //      System.debug('LScheckforDefaultIntPayment() >>> tsid_lsListMap size='+tsid_lsListMap.size()+''+ tsid_lsListMap);
        //        LSClass.checkforDefaultIntPayment(tsid_lsListMap);
    }   
}
  • You are definitely right that it's much easier and cleaner to use containsKey to build the map. The concept solved my problem and thanks so much for your help!! – user1761980 Jun 17 '14 at 1: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.