function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sascha DeinertSascha Deinert 

Two Aggregate Results in one list

Hi,

I've tried to add two aggregate results in one list, but I don't know if its correct because I don't know how can I show the results at the visualforce page.

Just for testing....
What is my plan, I want to count the number of accounts grouped by account Id, this make no sense but for the test its ok and on the other hand I want to count the number of sales and the sum of the sales grouped by account Id. The both aggregate results should be maped into one list.

Could you tell me is my code correct and how can I display the results?
 
Public Class VTP32_class {

    public String CombinedSummary { get; set; }

Public VTP32_class() {

}

Public class CombinedSummary {
    public Integer CountAcc {get; set;}
    public Id UId {get; set;} 
    public Integer CountSales {get; set;}
    public Double Brutto_BWS {get; set;}  
    
Public CombinedSummary (AggregateResult arM, AggregateResult arS) {
    CountAcc = (Integer) arM.get('CountAcc');
    UID = (Id) arS.get('Id');
    CountSales = (Integer) arS.get('CountSales');
    Brutto_BWS = (Double) arS.get('Brutto_BWS');
    }
}

Public List<CombinedSummary> generateWrappers() {
        Map<Id, AggregateResult> AccMap = new Map<Id, AggregateResult>();
        Map<Id, AggregateResult> AccSales = new Map <Id, AggregateResult>();

        List<CombinedSummary> combinedSummaries = new List<CombinedSummary>();
      
        FOR(AggregateResult arM: [SELECT COUNT(OwnerId) CountAcc, Id FROM Account GROUP BY Id]) {
            AccMap.put((Id)arM.get('Id'), arM);
        }
       
        FOR(AggregateResult arS: [SELECT COUNT(Id) CountSales, SUM(BWS_Brutto__c) Brutto_BWS, Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__r.Id FROM Umsatz__c GROUP BY Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__r.Id]) {
            AccSales.put((Id)arS.get('Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__r.Id'), arS);
        }
            
        Set<Id> AccSet = new Set<Id>();
        AccSet = AccSales.keySet();
        AccSet.addAll(AccSales.keySet());
        
        FOR (Id Id : AccSet) {
            combinedSummaries.add(new CombinedSummary(AccMap.get(Id), AccSales.get(Id)));
        }
        
        Return combinedSummaries;
    
}

}
Thanks,
Sascha
Adilson Arcoverde JrAdilson Arcoverde Jr
Sacha,

I think the best approach is to use a Map instead of using a List. For example, using a Map you could get a value using this code below (just an example ok?):
 
<apex:outputField value='{!generateWrappers['Sales'].<your_ar_field>}'/>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Regards.
Sascha DeinertSascha Deinert
Adilson, thanks for your reply, but I use map in combination with list.

If I tried to display I get the following error:
Error: Expression of type Text cannot be subscripted.
<apex:page controller="VTP32_class" sidebar="false">   

<apex:repeat value="{!generateWrappers}" var="var">
    <apex:outputField value="{!var['AccSales'].CountSales}"/>
</apex:repeat>

</apex:page>

 
Adilson Arcoverde JrAdilson Arcoverde Jr
Sacha,

Since you defined generateWrappers as a Map, you don't need an interation. So, your code shoud be:
 
<apex:page controller="VTP32_class" sidebar="false">   

<!-- Have to replace outputField to outputText since CountSales is not a SObjectField
<apex:outputText value="{!generateWrappers['AccSales'].CountSales}"/>

</apex:page>

 
Sascha DeinertSascha Deinert
Adilson, I get the same error :-(
<apex:page controller="VTP32_class" sidebar="false">   

    <apex:outputText value="{!generateWrappers['AccSales'].CountSales}"/>
 
</apex:page>

 
Adilson Arcoverde JrAdilson Arcoverde Jr
Sacha,

Could you send the entire code? I'll try to fix it.

Check out this code bellow (an example ok?):
// Controller
    public Map<String,Wrapper> mapTest = new Map<String,Wrapper> {
        'key 1' => new Wrapper(10)
    };

    public class Wrapper {
        public Decimal value {get;set;}

        public Wrapper( Decimal value ) {
            this.value = value;
        }
    }

// VF page

<apex:outputField value="{!mapTest['key 1'].value}"/>

It's working fine to me.

Regards.

 
Sascha DeinertSascha Deinert
Adilson, 

below you will find the test code. If I try to save the page I get the error:
Unknown property 'test_class.mapTest'
 
public class test_class {
   
   public Map<String,Wrapper> mapTest = new Map<String,Wrapper> {
        'key 1' => new Wrapper(10)
    };

    public class Wrapper {
        public Decimal value {get;set;}

        public Wrapper( Decimal value ) {
            this.value = value;
        }
    }

}
 
<apex:page controller="test_class" sidebar="false">   

<apex:outputField value="{!mapTest['key 1'].value}"/>

</apex:page>