Skip to main content
Hi,

I have a list that I want to split up into seperate sub groups.  Basically it's an aggregate result list that I want to split up by account name, and then time period (which I define as a string of year plus quarter).  Here is what I have so far, but I have to think there's a better way.

Thanks,

 

Map<string, Decimal> amountMap = new Map<String, Decimal>();

string lastname;

string name;

string accountId;

try {

for (AggListWrapper wrapper:wrapperList) {

name = wrapper.name;

accountId = wrapper.accountId;

decimal amount = wrapper.amount;

string periodYear = ''+ wrapper.year + '-' + wrapper.period;

periodYearSet.add(periodYear);

if (lastName == NULL) { lastName=name;}

if (lastName != name) {

completeRowList.add(new PivotRowWrapper(lastName, accountId, amountMap, NULL));

amountMap = new Map<string, Decimal>();

}

amountMap.put(periodYear, amount);

system.debug('PivotTimeSeries_Class *** account id = ' + accountId + 'end of loop name=' + name + ' lastName=' + lastName + ' amountMap=' + amountMap);

lastName = name;

}

} catch (exception e) {

system.debug('PivotTimeSeries_Class *** error in the wraperList lopp ' + e);

}

periodYearList.addAll(periodYearSet);

periodYearList.sort();

completeRowList.add(new PivotRowWrapper(name, accountId, amountMap, NULL)); // line total is null when we first go through

 
2 answers
  1. Nov 5, 2015, 1:53 AM
    I don't really know what your wrapper provides or what you're using it for, but here is how I would build a mapping of account names to periodYear to amount

     

    Map<String, Map<String, Decimal>> accountMap = new Map<String, Map<String, Decimal>>();

    for (AggListWrapper wrapper : wrapperList) {

    String name = wrapper.name;

    Id accountId = wrapper.accountId;

    Decimal amount = wrapper.amount;

    String periodYear = '' + wrapper.year + '-' + wrapper.period;

    if (!accountMap.containsKey(name)) {

    accountMap.put(name, new Map<String, Decimal>());

    }

    accountMap.get(name).put(periodYear, amount);

    }

    List<String> sortedAccountNames = new List<String>();

    sortedAccountNames.addAll(accountMap.keySet());

    sortedAccountNames.sort();

    This will generate a map that you can travers using the sortedAccountNames list.  If this doesn't help you, please help me understand your code and include the missing class definitions and possibly the usage of this code (Visualforce page?)
0/9000