Find your content:

Search form

You are here

Help summing columns in a datatable

 
Share

I am creating a PDF report which has 3 columns productId, SupplySize, and qty. I would like to sum the qty depending on the SupplySize. and display the values somewhere outside the table. This will be nice to have because the guys from the warehouse can easily know the qty of the boxes by size, instead of going row by row in the table.

for example if I have 5 boxSizes(7x7x5) and 5 different qtys and 2 boxSizes(10x10x5) with 2 different qtys. This will be displayed in the PDF grid in 7 different rows. And the ToTal Boxes Sizes will be something like this [boxSizes(7x7x5) =total QTY] and [boxSizes(10x10x5) =total QTY]

I would like to know which will be the best approach to do this. Thank you!

here is the controller:

public class  SO_1_Controller {

    private ApexPages.StandardController std;

    public List<SO_Detail__c> SODetails {get; set;}  

    public SO_1_Controller(ApexPages.StandardController stdCtrl)     
    {      
        std=stdCtrl;      
        SODetails=[select id, Name, NRProducts__r.Supplies__r.BoxSize__c, Qty__c,  
                   from SO_Detail__c 
                   where OSO_ID__c=:std.getId() 
                   order by Name asc];          
    }

}

Attribution to: Carlos

Possible Suggestion/Solution #1

Assuming you are creating the PDF report using a Visualforce page and associated Apex controller...

As you are iterating over each product collect the SupplySize and Qty sum in a map.

E.g. This was written here so may contains typos, but hopefully it gives you the core idea.

Map<string, long> supplySizeToQtySumMap = new Map<string, long>();
for(SO_Detail__c product : this.SODetails) {
    long qtySum = 0;
    string supplySize = product.NRProducts__r.Supplies__r.BoxSize__c;
    if(supplySizeToQtySumMap.containsKey(supplySize)) {
        qtySum = supplySizeToQtySumMap.get(supplySize);
    }
    qtySum += product.Qty__c;
    supplySizeToQtySumMap.put(product.supplySize, qtySum);
}

// Now loop through each product in the top level Map and pull out the quantity per SupplySize per product.

for(SO_Detail__c product : this.SODetails) { 
    string supplySize = product.NRProducts__r.Supplies__r.BoxSize__c;
    System.debug('Product Supply Size: ' + supplySize + ' Quantity: ' + supplySizeToQtySumMap.get(supplySize));
}

Attribution to: Daniel Ballinger
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30134

My Block Status

My Block Content