0

I have two objects (one custom object "sales", two standard object "opportunity" and "account") and I have three lists with aggregateresults.

Can I merge these lists into one list?

The goal of the list is to give the sales reps a good overview of the current situation of the accounts around the planning.

Apex

Public Class Plan220219_class {

    Public Summary1[] Summaries1 {get; set;}
    Public Summary2[] Summaries2 {get; set;}
    Public Summary3[] Summaries3 {get; set;}

    Public Plan220219_class() {

    agg1();

    agg2();

    agg3();

    }

    public void agg1() {
    AggregateResult[] results1 = [SELECT COUNT(OwnerId) CountAcc, Id FROM Account GROUP BY Id];
        Summaries1 = new List<Summary1>();
        FOR(AggregateResult ar1 : results1) {
            Summaries1.add(new Summary1(ar1));
        }
    }

    public void agg2() {
    AggregateResult[] results2 = [SELECT COUNT(Id) CountSales, SUM(Sales_amount__c) Sales_amount, contract2__r.contract1__r.account__r.Id FROM Sales__c GROUP BY contract2__r.contract1__r.account__r.Id];
        Summaries2 = new List<Summary2>();
        FOR(AggregateResult ar2 : results2) {
            Summaries2.add(new Summary2(ar2));
        }
    }

    public void agg3() {
    AggregateResult[] results3 = [SELECT SUM(Amount) Opp_Amount, Account.Id FROM Opportunity GROUP BY Account.Id];
        Summaries3 = new List<Summary3>();
        FOR(AggregateResult ar3 : results3) {
            Summaries3.add(new Summary3(ar3));
        }
    }

    public class summary1 {
        public Integer CountAcc {get; set;}
        public Id UId {get; set;}         

    public summary1 (AggregateResult ar1) {
        CountAcc = (Integer) ar1.get('CountAcc');
        UId = (Id) ar1.get('Id');       
        }
    }

    public class summary2 {
        public Integer CountSales {get; set;}
        public Double Sales_Amount {get; set;}      
        public Id UId {get; set;}

    public summary2 (AggregateResult ar2) {
        CountSales = (Integer) ar2.get('CountSales');
        Sales_Amount = (Double) ar2.get('Sales_amouont');        
        UId = (Id) ar2.get('Id');
        }
    }

    public class summary3 {
        public Double Opp_Amount {get; set;}      
        public Id UId {get; set;}

    public summary3 (AggregateResult ar3) {
        Opp_Amount = (Double) ar3.get('Opp_Amount');        
        UId = (Id) ar3.get('Id');
        }
    }

}

Visualforce

<apex:page controller="Plan220219_class">

    <apex:repeat value="{!Summaries1}" var="summary1">
        {!summary1.UId} : {!summary1.CountAcc} <br/>
    </apex:repeat>

    <br/>

    <apex:repeat value="{!Summaries2}" var="summary2">
        {!summary2.UId} : {!summary2.CountSales} : {!summary2.Brutto_BWS} <br/>
    </apex:repeat>

    <br/>

    <apex:repeat value="{!Summaries3}" var="summary3">
        {!summary3.UId} : {!summary3.OPP_Amount} <br/>
    </apex:repeat>

</apex:page>

RESULT Visualpage

List1 account overview:

0018E00000UqpQFQAZ : 1

0018E00000UqqZvQAJ : 1

0018E00000f8iJAQAY : 1

0018E00000XIp5hQAD : 1

0018E00000XrFPPQA3 : 1

0018E00000eMraBQAS : 1

0018E00000UqpQAQAZ : 1

0018E00000f5gMQQAY : 1

List2 sales of the accounts:

0018E00000UqpQFQAZ : 2 : 32000.0

0018E00000UqqZvQAJ : 2 : 453000.0

0018E00000UqpQAQAZ : 2 : 20000.0

List3 opps of the accounts:

0018E00000UqpQFQAZ : 1000.0

0018E00000eMraBQAS : 0.0

0018E00000UqpQAQAZ : 1000.0

0

Yes, you can. The usual route is to use a wrapper class pattern, which you actually already have going here. Your summary classes, like this one,

public class summary2 {
    public Integer CountSales {get; set;}
    public Double Sales_Amount {get; set;}      
    public Id UId {get; set;}

public summary2 (AggregateResult ar2) {
    CountSales = (Integer) ar2.get('CountSales');
    Sales_Amount = (Double) ar2.get('Sales_amouont');        
    UId = (Id) ar2.get('Id');
    }
}

are really wrapper classes.

The way I would approach this is to define a new class, say CompleteSummary, that includes all of the summarized data points:

public class CompleteSummary {
    public Integer CountSales {get; set;}
    public Double Sales_Amount {get; set;}  
    public Integer CountAcc {get; set;}  
    public Double Opp_Amount {get; set;}        
    public Id UId {get; set;}

}

Then, build up a Map<Id, CompleteSummary> as you iterate through each of your AggregateResult lists. Each time through the loop, you can use your UId as a key to see if you've started building a CompleteSummary in the Map for this Id yet. If you have, you just add the keys from this sort of AggregateResult to that object; if not, you start a new CompleteSummary.

When you're done aggregating information, you can store a List<CompleteSummary> (just summaryMap.values(); you can have your wrapper class implement Comparable if you want to sort them), and display that list on your Visualforce page.

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.