Find your content:

Search form

You are here

I have problem displaying List values in a repeat and no show duplicate values

 
Share

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>

Attribution to: Carlos

Possible Suggestion/Solution #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);
        }
    }
}

Attribution to: JCD

Possible Suggestion/Solution #2

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


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

My Block Status

My Block Content