1

I have an aggregateResult query which returns the aggregates(MAX) of few fields and I display that in apex:dataTable in the Visualforce page. This is fine.

Now I need to run this aggregateResult query multilple times (changing the WHERE condition). So I put the entire code in the for loop. But the result is showing only the last aggregated results instead of all the results in multiple rows.

Below is the code

public with sharing class SnapshotDeliveryTonnageTable{

    Apexpages.StandardController controller;
    public SnapshotDeliveryTonnageTable(ApexPages.StandardController c) {                
        controller = c;   
    }
public List<SnapshotDeliveryTonnageTable> calcSharePrev = new List<SnapshotDeliveryTonnageTable>();
    public List<SnapshotDeliveryTonnageTable> getCalcPrevShares(){
     calcSharePrev.clear();
        for(String last12Months:xNode){
            //aggregates of all,
            AggregateResult[] groupedResultsAllCalc = [SELECT Supplier_Name__c,SUM(Modified_PRC_Won__c),SUM(Modified_BPC_Won__c),SUM(Modified_CAB_Won__c),SUM (Modified_BAR_Won__c),SUM(Modified_MESH_Won__c),SUM(Total_Steel__c) FROM Supplier_Snapshot__c WHERE Month__c=:last12Months GROUP BY ROLLUP(Supplier_Name__c)];
            if(groupedResultsAllCalc.size()>0){
                for(AggregateResult ar : groupedResultsAllCalc){
                    //I do calculation
                }
            }   
            //I assign the values to the variables and then ADD it to the dataTable output . 4 columns
            tcalcNamePrev=last12months;
                    calcSharePrev.add(new SnapshotDeliveryTonnageTable(tcalcNamePrev,Major1CalcGRANDShare,Major2CalcGRANDShare,Major3CalcGRANDShare));                                       
        }
        return calcSharePrev;
    }
}

So, in the output data table it is showing only one row and that is the last row ( last12Months variable is showing the last value in the table).

To further test it, I have added a dummy line to the calcSharePrev at the end. Then only this dummy line is showing in the output datatable.

tcalcNamePrev='abc';
Major1CalcGRANDShare=123;
Major2CalcGRANDShare=456;
Major3CalcGRANDShare=789;
calcSharePrev.add(new SnapshotDeliveryTonnageTable(tcalcNamePrev,Major1CalcGRANDShare,Major2CalcGRANDShare,Major3CalcGRANDShare));        

So, only one line is showing in the dataTable. How do I add multiple lines to calcSharePrev. (Each line is an instance of the class)

Please help!

EDIT: Yes, I clear the calcSharePrev at the start of the method. This method is called on a button click. if I dont clear, everytime the button is clicked, the rows are adding up and repeating.

  • Where is calcSharePrev being instantiated? That code seemes to be absent but I suspect it's being cleared in the loop or you're creating a new list each time through. – Matt Lacey Jan 23 '14 at 6:15
  • Yes, I clear the calcSharePrev at the start of the method. This method is called on a button click. if I dont clear, everytime the button is clicked, the rows are adding up and repeating. – sfdcFanBoy Jan 23 '14 at 7:29
  • 2
    Your returning calcNSHSharePrev, but adding items to calcSharePrev, is this correct? – Andrew Fawcett Jan 23 '14 at 9:10
  • Thanks Andrew. I missed it. I have updated the code. Now it returns 12 rows as expected BUT all the rows are the same. It is displaying only the last month data 12 times! – sfdcFanBoy Jan 27 '14 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.