1

I can display values from a List in VF Page 'repeat', in the System.debug() the list is displaying the values. I have two question: why my vf is empty and also is there any way to NO show duplicate values for example I am getting:

USER_DEBUG [50]|DEBUG|(7x4x4 Qty 54) 
USER_DEBUG [50]|DEBUG|(7x4x4 Qty 54) 
USER_DEBUG [50]|DEBUG|(10x4x5 Qty 20) 
USER_DEBUG [50]|DEBUG|(10x4x5 Qty 20) 

Below is my controller code:

public class  SO_1_Controller {
    private ApexPages.StandardController std;
    public List<SO_Detail__c> SODetails {get; set;} 
    public string values{get;set;}
    public string supplySize2{get;set;}
    public List<String> List1{get;set;}

    public SO_1_Controller(ApexPages.StandardController stdCtrl) {
        std=stdCtrl;

        SODetails=[select id, Name, X1__c, NRProducts__r.Supplies__r.Size__c
                    from SO_Detail__c
                    where OSO_ID__c=:std.getId() 
                    order by Name asc]; 

        Map<String, Decimal> supplySizeToQtySumMap = new Map<String, Decimal>();

        for ( SO_Detail__c product : this.SODetails ) {
            Decimal qtySum = 0;
            String supplySize = product.NRProducts__r.Supplies__r.Size__c;

            if(supplySizeToQtySumMap.containsKey(supplySize)) {
                qtySum = supplySizeToQtySumMap.get(supplySize);
            }

            qtySum += product.X1__c;
            supplySizeToQtySumMap.put(supplySize, qtySum); 
        }

        for ( SO_Detail__c product : this.SODetails ) { 
            supplySize2 = product.NRProducts__r.Supplies__r.Size__c;
            values =  supplySize2 + ' Qty ' +   String.valueof(supplySizeToQtySumMap.get(supplySize2));
            List<String> list1 = new List<String>();
            List1.add(values);
            System.debug(List1);            
        }
    }  

    public List<String> getList1() {
        if ( List1 != NULL )
            return List1;
        else
            return null;
    }
}

Visual force page to display the total box size:

<apex:outputPanel layout="block" styleClass="LinesTable2">
    <table id="apppp">
        <tr><th>Total Boxes</th></tr>
        <apex:repeat var="d" value="{!List1}">
            <tr><td><apex:outputText value="{!d}"/></td></tr>
        </apex:repeat>
    </table>
</apex:outputPanel>
  • Can you post your Visualforce code? – Daniel Hoechst Mar 19 '14 at 16:30
  • Hello Daniel, JCD just posted it. Or do you want the whole code? – Carlos Mar 19 '14 at 16:45
  • No, that's enough. – Daniel Hoechst Mar 19 '14 at 16:49
  • I also tried pageBlockTable but no luck – Carlos Mar 19 '14 at 16:52
1

I think part of the problem may be that you're creating a local list1 variable right before adding your values to List1. Because Apex is case-insensitive, it's adding your values to the local variable instead of the member variable that you're intending to use:

List<String> list1 = new List<String>();
List1.add(values);
System.debug(List1); 

You really don't need this local variable at all, so let's remove it. As for the duplicate values, in your second loop you can iterate through the set of keys in your map instead of going through the queried results again. Keep in mind that they will be arbitrarily ordered if you use this approach, however. Give this a shot (I also removed a few other unnecessary member variables and your getter method, since there wasn't any additional logic in it):

public class  SO_1_Controller {
    private ApexPages.StandardController std;
    public List<String> List1 {public get; private set;}

    public SO_1_Controller(ApexPages.StandardController stdCtrl) {
        std = stdCtrl;

        SO_Detail__c[] SODetails = [select id, Name, X1__c, NRProducts__r.Supplies__r.Size__c
                    from SO_Detail__c
                    where OSO_ID__c = :std.getId() 
                    order by Name asc]; 

        Map<String, Decimal> supplySizeToQtySumMap = new Map<String, Decimal>();

        for ( SO_Detail__c product : SODetails ) {
            Decimal qtySum = 0;
            String supplySize = product.NRProducts__r.Supplies__r.Size__c;

            if ( supplySizeToQtySumMap.containsKey(supplySize) ) {
                qtySum = supplySizeToQtySumMap.get(supplySize);
            }

            qtySum += product.X1__c;
            supplySizeToQtySumMap.put(supplySize, qtySum); 
        }

        List1 = new List<String>();
        for ( String supplySize : supplySizeToQtySumMap.keySet() ) {
            String value = supplySize + ' Qty ' + String.valueOf(supplySizeToQtySumMap.get(supplySize));
            List1.add(value);
            System.debug(List1);
        }
    }
}
  • ok I had to add List1=new List<String>(); before List1.add(values); and now I can see the last value of the list USER_DEBUG [50]|DEBUG|(10x4x5 Qty 20) but no the other ones. – Carlos Mar 19 '14 at 19:17
  • Good call, looks like I left that line out. You'll want to add that line just before the for loop, otherwise you'll keep overwriting your member variable with every iteration of the loop. Please see my updated answer. – JCD Mar 19 '14 at 19:30
  • great!!.Now is displaying everything..thanks a lot. is there any way to avoid the duplicates values? these are values that I am getting 7x4x4 Qty 54 5x4x4 Qty 56 7x4x4 Qty 54 7x4x4 Qty 54 5x4x4 Qty 56 – Carlos Mar 19 '14 at 19:36
  • JCD and Daniel, thank you so much for helping me. I am surprise with all the help I got from this site and the amazing people that are here. – Carlos Mar 19 '14 at 19:44
  • You can use Daniel's approach with a Set member variable, or loop through the map's keyset instead of looping back through the queried records. See my updated answer. – JCD Mar 19 '14 at 19:44
3

To avoid duplicates, you can change to using a Set instead of list. Sets are unique but not ordered.

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.