3

I have seven different recordtypes, but it seems that the first "for" (FOR (RecordType RType : RTypeList) loop run just once. The debug log show that the sql find 7 rows.

09:22:47.089 (89771210)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|SELECT Id, Name FROM RecordType WHERE SobjectType = 'Opportunity'
09:22:47.124 (124132161)|SOQL_EXECUTE_END|[11]|Rows:7

Public Class VTPTeam3_class {

    Public Summary[] Summaries {
        get;
        set;
    }

    Public VTPTeam3_class() {

    }

    Public void getInformation() {

        List < RecordType > RTypeList = [SELECT Id, Name FROM RecordType WHERE SobjectType = 'Opportunity'];
        FOR(RecordType RType: RTypeList) {

            AggregateResult[] Results = [SELECT Account.Owner.Name accOwner, RecordType.Name oppProduct, SUM(Amount) PlanValue FROM Opportunity WHERE RecordTypeId = : RType.Id GROUP BY Account.Owner.Name, RecordType.Name];
            Summaries = new List < Summary > ();
            FOR(AggregateResult ar: Results) {
                Summaries.ADD(new Summary(ar));
            }
        }

    }

    Public Class Summary {
        Public String Ownername {
            get;
            set;
        }
        Public String Productname {
            get;
            set;
        }
        Public Double Value {
            get;
            set;
        }

        Public Summary(AggregateResult ar) {
            Ownername = (String) ar.get('accOwner');
            Productname = (String) ar.get('oppProduct');
            Value = (Double) ar.get('PlanValue');
        }
    }

}
1
  • How are you sure about the loop running just once?
    – Saroj Bera
    Jul 14, 2015 at 8:24

1 Answer 1

7

The problem is probably that you re-initilaise the Summaries list every time round the loop to an empty list so you end up with only 1 item in the final list.

This is probably what you want:

   Summaries = new List < Summary > ();
   FOR(RecordType RType: RTypeList) {
        AggregateResult[] Results = [SELECT Account.Owner.Name accOwner, RecordType.Name oppProduct, SUM(Amount) PlanValue FROM Opportunity WHERE RecordTypeId = : RType.Id GROUP BY Account.Owner.Name, RecordType.Name];
        FOR(AggregateResult ar: Results) {
            Summaries.ADD(new Summary(ar));
        }
    }

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.