Solved

How to aggregate values in a list by specific elements

Posted on 2014-02-07
3
168 Views
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;
        listToInsert.add(obj);
    }
 fundName = obj.FundName__c;
 contactId = obj.Contact__c;
}

Open in new window


Any help is appreciated.
Thanks.
0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
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'
Sales.java
SalesWrapper.java
0
 

Author Comment

by:-Dman100-
ID: 39843154
Hi CEHJ,

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!
Regards.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn how the use of a bunch of disparate tools requiring a lot of manual attention led to a series of unfortunate backup events for one company.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

763 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