How to aggregate values in a list by specific elements

Posted on 2014-02-07
Medium Priority
Last Modified: 2016-02-17
I'm using the salesforce (apex) language, so I'm posting in the java group since apex is a derivative of the java language.  I hope someone can help.

I have a batch program I'm working on that contains a list of sobjects that haven't been inserted and I need to loop over the list and find the elements in the list that have the same fund and contact and aggregate several fields. Once I have the new list, then I do the insert.

I've been struggling on how to correctly loop over my list to find the same elements by fund and contact and then do the aggregation of the currency fields? The first issue is that the list isn't sorted so how do I find the correct elements to aggregate the values?

This is the pattern I was tyring, but it doesn't work.

List<My_SObject__c> listToInsert = new List<My_SObject__c>();
String fundName = '';
Id contactId = null;
for(My_SObject__c obj : tempList) {
    if(obj.FundName__c = fundName && obj.Contact__c == contactId) {
        obj.MyCurrencyFieldToAggregate__c += obj.MyCurrencyFieldToAggregate__c;
 fundName = obj.FundName__c;
 contactId = obj.Contact__c;

Open in new window

Any help is appreciated.
Question by:-Dman100-
  • 2
LVL 86

Expert Comment

ID: 39842901
You could aggregate by using Set operations with a wrapper class. Better to demonstrate: run the following and note the aggregation by 'fund' and 'contact'

Author Comment

ID: 39843154

Thanks for the example, I'm looking at the code and trying to decipher what it is doing.  Can you explain the aggregation portion?  Is the aggregation occurring in the SalesWrapper class in the two overridden methods?

Thanks for your help!  I very much appreciate it!
LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 39843201
The aggregation is occurring as a result of the wrapper class implementing equals using the key criteria of fund and contact. It ignores any other attributes. Equality is met by fund and contact matching

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Use this step by step method when setting up QuickBooks Online. They will allow you to explore the various features of the advanced settings available to you.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This Micro Tutorial will explain how to export DynamoDB tables in Amazon Web Services.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question