0

I have a Map where I put a String and a Decimal. Both of these fields are updated based on the day of the week. The String field can be converted into a date field since it's populated by the day, month and year of the day the corresponding Decimal field has been modified.

For example : attendanceRate_2__c modified on 10-28-2019 techDayMonthYear_2__c will be populated with 10282019

AttendanceRate_3__c modified on 10-29-2019 TechDayMonthYear_3__c will be populated with 10292019

The 'AttendanceRate_' + weekDay + '__c' fields are populated thanks to the day of the week. 2 for monday, 3 for tuesday ect.

I'm changing dynamically the weekDay in my apex code but the issue is that the new value replace the one that was put in the Map a week prior.

attendanceRate_2__c value on 10-28-2019 will be replaced by the value of attendanceRate_2__c on 11-04-2019

Here's my code:

    Map<Integer, Map<String, Decimal>> workedDaysByMonth =  new Map<Integer, Map<String, Decimal>>();


        List<AttendanceRate__c > allAttendanceRates = [SELECT Id, User__c, AttendanceRate_2__c, AttendanceRate_3__c, 
                                                     AttendanceRate_4__c, AttendanceRate__c, AttendanceRate_6__c, 
                                                     techDayMonthYear_2__c, techDayMonthYear_3__c, techDayMonthYear_4__c,
                                                     techDayMonthYear_5__c, techDayMonthYear_6__c, Tech_External_Id__c
                                                     FROM AttendanceRate__c 
                                                     WHERE User__c  = :this.u.Id];

        for(AttendanceRate__c ar: allAttendanceRates){
            for(Integer weekDay = 2; weekDay <= 6; weekDay ++){
                workedDaysByMonth.put(weekDay, new Map<String, Decimal>{ (String) tp.get('techDayMonthYear_' + weekDay + '__c') => (Decimal) getNotNullValue(tp.get('AttendanceRate_'+ weekDay + '__c'))});

            }                              
        }

I don't know what I'm doing wrong...

1 Answer 1

0

You're overwriting your child Map:

workedDaysByMonth.put(
    weekDay, 
    new Map<String, Decimal>{ 
        (String) tp.get('techDayMonthYear_' + weekDay + '__c') => (Decimal) getNotNullValue(tp.get('AttendanceRate_'+ weekDay + '__c'))
    }
);

For each record in allAttendanceRates, you overwrite the Map stored under the key weekDay with a brand-new one.

If you want to accumulate values across iterations, you'll need to check whether there's already a child map, and add a value to it if so. The pattern in pseudocode looks like this:

Map<Id, Map<Some, Other>> parentMap = new Map(...);
for (someObject__c s: someList) {
    if (!parentMap.containsKey(s.Category__c)) {
        parentMap.put(s.Category__c, new Map<Some, Other>());
    }
    parentMap.get(s.Category__c).put(s.Key__c, s.Value__c);
}

You only create the child map once, and thereafter add values to it as you iterate.

1
  • Yes worked perfectly ! thank you
    – chloe
    Oct 31, 2019 at 8:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .