function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
claperclaper 

Help with PageblockTable and Select List

Hi!

 

Now i'm working on a VF page that in one column has a select list and allows the user to select a value from the list and reRender a cross-tabular report on the same page. The issue comes with the picklists, the controller only gets the last value of the pageblocktable picklist. 

 

any help? pls!

=============================================================================================

here is the VF page:

<apex:page standardcontroller="account" extensions="mtc_TerritoryDesigner2" readOnly="true">
<script>
function hi(){
return "hi";
}
</script>
<apex:form >
        <apex:SectionHeader title="MTC Overview" subtitle="MTC"/>
        <apex:toolbar id="toolbar" style="background-color:#EEECD1;background-image:none;" height="20px;">
            <apex:toolbarGroup >
            <apex:commandLink value="Territory Designer"/>
            <apex:commandLink value="Manager Grid"/>
            <apex:commandLink value="SA Grid"/>
            <apex:commandLink value="Industry Grid" />
            <apex:commandLink value="Detail Assigment"/>
            <apex:commandLink value="MTC Overview"/>
            </apex:toolbarGroup>            
        </apex:toolbar>   
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageblocktable value="{!Accounts}" var="arr" id="accounts">
                    <apex:column >
                        <apex:facet name="header">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</apex:facet>
                        <apex:facet name="footer">Totals</apex:facet>
                        <apex:outputText value="{!arr.UserName}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Customer</apex:facet>
                        <apex:facet name="footer"><apex:outputText value="{!FinalTotalCustomers}" /></apex:facet>
                        <apex:outputText value="{!arr.TotalCustomers}" >                            
                        </apex:outputText>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Non Customer</apex:facet>
                        <apex:facet name="footer"><apex:outputText value="{!FinalTotalNonCustomers}" /></apex:facet>
                        <apex:outputText value="{!arr.TotalNonCustomers}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Open Coverage</apex:facet>  
                        <apex:facet name="footer"><apex:outputText value="{!FinalTotalOpenCoverages}" /></apex:facet>               
                        <apex:outputText value="{!arr.TotalOpenCoverages}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of MIF</apex:facet>   
                        <apex:facet name="footer"><apex:outputText value="{!FinalTotalEquipment}" /></apex:facet>                  
                        <apex:outputText value="{!typeSelected}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Leases</apex:facet> 
                        <apex:facet name="footer"><apex:outputText value="{!FinalTotalLease}" /></apex:facet>  
                        <apex:outputText value="{!conttype}" />                   
                    </apex:column>
                </apex:pageblocktable>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Accounts by Zip" id="SecondSection" columns="1" >
        <apex:pageMessages ></apex:pageMessages>
                    <script>
                        j$('#dialogPopup').dialog('close');
                    </script>
                <apex:pageBlockTable value="{!AccountsByZip}" var="ar" styleClass="sortable" id="accountsbyzip">
                    <apex:column >
                        <apex:facet name="header">Zip</apex:facet>
                        <apex:outputText value="{!ar.Zip}" /> 
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Owner</apex:facet>
                        <apex:outputText value="{!ar.ownerName}" >
                        </apex:outputText> 
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Establishments</apex:facet>
                        <apex:outputText value="{!ar.NumAccounts}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Customers</apex:facet>
                        <apex:outputText value="{!ar.NumCustomers}" >                           
                        </apex:outputText>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Non Customers</apex:facet>
                        <apex:outputText value="{!ar.NumNonCustomers}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header"># of Open Coverage</apex:facet>
                        <apex:outputText value="{!ar.NumOpenCoverages}" />
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Reassign</apex:facet>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Type</apex:facet>
                        <apex:selectList value="{!typeselected}" size="1">
                            <apex:selectOptions value="{!ar.AccTypex}"/>                      
                        </apex:selectList>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Preview</apex:facet>
                        <apex:commandButton value="Preview" style="width: 100px;" action="{!dofetchAccounts}" rerender="accounts" >
                            <apex:param name="contCustomers" value="{!ar.NumCustomers}" assignTo="{!contCustomers}"/>
                            <apex:param name="contNonCustomers" value="{!ar.NumNonCustomers}" assignTo="{!contNonCustomers}"/>
                            <apex:param name="contOpenCoverages" value="{!ar.NumCustomers}" assignTo="{!contOpenCoverages}"/>
                            <apex:param name="contName" value="{!ar.OwnerName}" assignTo="{!contName}"/>  
                            <apex:param name="contType" value="{!ar.typeselected}" assignTo="{!contType}"/>                                           
                        </apex:commandButton>
                    </apex:column> 
                    <apex:column >
                        <apex:facet name="header">Processing</apex:facet>
                        <apex:commandButton value="Processing"/>
                    </apex:column>                  
                </apex:pageBlockTable>                            
                </apex:pageBlockSection>
       </apex:pageblock>
   </apex:form>
</apex:page>

 here is the controller:

public class mtc_TerritoryDesigner2 {

    private ApexPages.StandardController stdCtrl {get; set;}
    public list<AggregateResult> listAr = new list<AggregateResult>();
    public list<AggregateResult> listAr2 = new list<AggregateResult>();
    String CurrentUser = UserInfo.getUserId();
    String currentUserRole = UserInfo.getUserRoleId();
    List<UserRole> RepRole = [Select id from UserRole where parentroleid =: currentUserRole];
    List<id> RepsId = new List<id>();
    List<User> Users = [select name from User where isactive = true AND userroleid =: RepRole order by Name];
    Integer FinalCustomers, FinalNonCustomers, FinalOpenCoverages, FinalEquipment = 0, FinalLease = 0, ReassingCustomersAcc = 0, contCustomers2;

    public Integer getFinalTotalCustomers() { return finalCustomers;}
    public Integer getFinalTotalNonCustomers() {return finalNonCustomers;}
    public Integer getFinalTotalOpenCoverages() { return finalOpenCoverages;}
    public Integer getFinalTotalEquipment() { return finalEquipment;}
    public Integer getFinalTotalLease() { return finalLease;}
    public List<accls> Accounts2 = new List<accls>();
    public List<accByZip> Accounts3 = new List<accByZip>();
    
    
    public List<Accls> Accounts {get{return accounts2;} set;}
    public List<AccByZip> AccountsByZip {get{return accounts3;} set;}
    
    //public Integer contCustomers {get; set;}
    public Integer ContCustomers {get;set;}
    public Integer ContNonCustomers {get;set;}
    public Integer ContOpenCoverages {get;set;}
    public String contName {get;set;}
    public String contType {get;set;}
    public String TypeSelected {get;set;}
    
    //public String getTypeSelected() {return this.TypeSelected;}
   // public void setTypeSelected(String s) {this.TypeSelected = s;}
    
    public mtc_TerritoryDesigner2(ApexPages.StandardController controller) {
        stdCtrl = controller;
        for(User u:Users)
        {
            RepsId.add(u.id);
        }
        fetchaccounts();
        fetchaccountsbyZip();
    }
    
    public List<SelectOption> fetchAccTypes() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('None','None'));
            options.add(new SelectOption('Customer','Customer'));
            options.add(new SelectOption('Non Customer','Non Customer'));
            options.add(new SelectOption('Open Coverage','Open Coverage'));
            options.add(new SelectOption('All','All'));
            
            return options;
        }
  
    private void fetchAccounts()
    {
        accounts2.clear();
        listAr = [select owner.name num, type, COUNT(id) total from account where ownerid =: RepsId GROUP BY CUBE (owner.name,type) order by owner.name]; 
        
        for(User u:Users){
            Accls objAccls = new Accls();
            for(AggregateResult ag: listAr)
            {
                
                if(String.valueof(ag.get('num')) != null && string.valueof(ag.get('type')) != null && String.valueof(ag.get('num')) == u.name){
                    if(ag.get('type') == 'customer'){
                        if((string)ag.get('num') == contName  )
                        {objAccls.totalCustomers = integer.valueof(ag.get('total')) + contCustomers ;}else{objAccls.totalCustomers = integer.valueof(ag.get('total'));}}
                    if(ag.get('type') == 'Non customer'){
                        objAccls.totalNonCustomers = integer.valueof(ag.get('total'));}
                    if(ag.get('type') == 'Open Coverage'){
                        objAccls.TotalOpenCoverages = integer.valueof(ag.get('total'));}else{objAccls.TotalOpenCoverages = 0;}
                    objAccls.UserName = String.valueof(ag.get('num'));  
                    
                    //objAccls.TotalEquipment = [Select count() from asset where accountid in :[Select id from account where owner.name =: u.name and isdeleted = false ]];                              
                    //objAccls.TotalLease = [Select count() from asset where accountid in :[Select id from account where owner.name =: u.name and isdeleted = false ] AND Price_Plan__c = 'lease'];                              
                }
                if(ag.get('type') == 'Open Coverage' && ag.get('num') == null){
                    FinalOpenCoverages = integer.valueof(ag.get('total'));}
                if(ag.get('type') == 'Non Customer' && ag.get('num') == null){
                    FinalNonCustomers = integer.valueof(ag.get('total'));}
                if(ag.get('type') == 'Customer' && ag.get('num') == null){
                    if(contCustomers > 0){
                    FinalCustomers = integer.valueof(ag.get('total')) + contCustomers;} else{FinalCustomers = integer.valueof(ag.get('total'));}}

            }Accounts2.add(objAccls);
        }
    }
    
    private void fetchAccountsByZip(){
        
        listAR2 = [SELECT owner.name num, shippingpostalcode, type, count(id) total FROM account where  ownerid =:RepsId AND isdeleted=false GROUP BY  shippingpostalcode,owner.name,type order by shippingpostalcode];
        
        for(AggregateResult u:[SELECT shippingpostalcode FROM account where  ownerid =:RepsId AND isdeleted=false GROUP BY  shippingpostalcode order by shippingpostalcode]){
            AccByZip objOppClass2 = new AccByZip();
            for (AggregateResult ar: listAR2)
            {
                if(String.valueof(ar.get('num')) != null && string.valueof(ar.get('type')) != null && String.valueof(ar.get('shippingpostalcode')) == (string)u.get('shippingpostalcode')){
                    if(ar.get('type') == 'Customer'){
                        objOppClass2.NumCustomers= (ar.get('total') != '' ? (integer.valueof(ar.get('total'))) : 0);}
                    if(ar.get('type') == 'Non Customer' && ar.get('total') != null){
                        objOppClass2.NumNonCustomers = (ar.get('total') != '' ? (integer.valueof(ar.get('total'))) : 0);}
                    if(ar.get('type') == 'Open Coverage' && ar.get('total') != null){          
                        objOppClass2.NumOpenCoverages = (ar.get('total') != '' ? (integer.valueof(ar.get('total'))) : 0 );}
                    objOppClass2.Zip = (String)ar.get('shippingpostalcode');
                    objOppClass2.ownerName = (String)ar.get('num');
                    objOppClass2.AccTypex = fetchAccTypes();
                    
                    if(ObjOppClass2.NumCustomers != null && ObjOppClass2.NumNonCustomers != null && ObjOppClass2.NumOpenCoverages != null){
                      objOppClass2.NumAccounts = ObjOppClass2.NumCustomers+ObjOppClass2.NumNonCustomers+ObjOppClass2.NumOpenCoverages;
                    }else{
                        if(ObjOppClass2.NumCustomers != null && ObjOppClass2.NumNonCustomers != null )
                        {
                            objOppClass2.NumAccounts = ObjOppClass2.NumCustomers+ObjOppClass2.NumNonCustomers;
                        }
                        else{
                            if(ObjOppClass2.NumCustomers != null && ObjOppClass2.NumOpenCoverages != null )
                            {
                                objOppClass2.NumAccounts = ObjOppClass2.NumCustomers+ObjOppClass2.NumOpenCoverages;
                            }
                            else{
                                if(ObjOppClass2.NumNonCustomers != null && ObjOppClass2.NumOpenCoverages != null )
                                {
                                    objOppClass2.NumAccounts = ObjOppClass2.NumNonCustomers+ObjOppClass2.NumOpenCoverages;
                                }
                                else{
                                    if(ObjOppClass2.NumCustomers != null && ObjOppClass2.NumNonCustomers == null && ObjOppClass2.NumOpenCoverages == null )
                                    {
                                        objOppClass2.NumAccounts = ObjOppClass2.NumCustomers;
                                    }
                                    else{
                                        if(ObjOppClass2.NumNonCustomers != null && ObjOppClass2.NumCustomers == null && ObjOppClass2.NumOpenCoverages == null )
                                        {
                                            objOppClass2.NumAccounts = ObjOppClass2.NumNonCustomers;
                                        }
                                        else{
                                            if(ObjOppClass2.NumOpenCoverages != null && ObjOppClass2.NumNonCustomers == null && ObjOppClass2.NumCustomers == null )
                                            {
                                                objOppClass2.NumAccounts = ObjOppClass2.NumOpenCoverages;
                                            }
                                        }
                                    }
                                }
                            }
                        }  
                    }
                }
           }Accounts3.add(objOppClass2);                      
        }
    }
    
    public PageReference dofetchAccounts()
    {
         
         fetchAccounts();
         return null;
    }
    

    public class Accls
    {
        public Integer TotalAccounts {get;set;}
        public Integer TotalCustomers {get;set;}
        public Integer TotalNonCustomers {get;set;}
        public Integer TotalOpenCoverages {get;set;}
        public Integer TotalEquipment { get; set;}
        public Integer TotalLease { get; set;}
        public String UserName {get;set;}    
        
                
    }
    
    public class AccByZip
    {
        public Integer NumAccounts{ get;set; }
        public String Zip{ get;set; }
        public String ownerName{ get;set; }
        public Integer NumCustomers{ get;set; }
        public Integer NumNonCustomers{ get;set; }
        public Integer  NumOpenCoverages{ get;set; }
        public List<SelectOption> Acctypex {get;set;}
        public String Acctypexx {get;set;}        
        
    }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
claperclaper

And here comes the solution!! I decided to post the code for others that might face the same issue. The trick was to rerender the pageblocktable once the user pick a value from the dropdown list. here is the code, high lighted in red.

 

VF PAGE:

<apex:pageBlockSection title="Establishments by Zip" id="SecondSection" columns="1" >
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockTable value="{!AccountsByZip}" var="ar" id="accountsbyzip" >
<apex:column >
<apex:facet name="header">Zip</apex:facet>
<apex:outputText value="{!ar.Zip}" />
</apex:column>
<apex:column >
<apex:facet name="header">Estab Rep</apex:facet>
<apex:outputText value="{!ar.ownerName}" >
</apex:outputText>
</apex:column>
<apex:column >
<apex:facet name="header"># of Establishments</apex:facet>
<apex:outputText value="{!ar.NumAccounts}" />
</apex:column> 
<apex:column >
<apex:facet name="header"># of Customers</apex:facet>
<apex:outputText value="{!ar.NumCustomers}" />
<apex:outputText value="0" rendered="{!if(ar.NumCustomers > 0 , false, true)}"/>
</apex:column>
<apex:column >
<apex:facet name="header"># of Non Customers</apex:facet>
<apex:outputText value="{!ar.NumNonCustomers}" />
<apex:outputText value="0" rendered="{!if(ar.NumNonCustomers > 0 , false, true)}"/>
</apex:column>
<apex:column >
<apex:facet name="header"># of Open Coverage</apex:facet>
<apex:outputText value="{!ar.NumOpenCoverages}" />
</apex:column>

//this is where the magic happens. {!RepOptions} contains all the values of the rep. then I use param tag to pass the value to a variable called contRepSelection on my controller, when the user changes the value of the picklist
<apex:column >
<apex:facet name="header">Reassign</apex:facet>
<apex:selectList value="{!ar.RepSelection}" size="1" id="repvalue" >
<apex:selectOptions value="{!RepOptions}"/>
<apex:actionSupport event="onchange" reRender="accountsbyzip" >
<apex:param value="{!ar.RepSelection}" name="contRepSelection" assignTo="{!contRepSelection}" />
</apex:actionSupport>
</apex:selectList>
</apex:column>

//This is the button that the user presses after changing the rep picklist, this button its located on the same row of the pageblocktable as the picklist. when the user press the button i used param value, to pass all the columns data for that row into the controller. NOTICE THAT I PASS AGAIN THE VALUE OF THE ar.Repselection TO THE contRepselection VARIABLE.  This is how i got it to work for me. 
<apex:column >
<apex:facet name="header">Preview</apex:facet>
<apex:commandButton value="Preview" style="width: 100px;" action="{!dofetchAccounts}" rerender="accounts, accountsbyzip" status="loading" id="btnpreview" >
<apex:param name="contCustomers" value="{!ar.NumCustomers}" assignTo="{!contCustomers}"/>
<apex:param name="contNonCustomers" value="{!ar.NumNonCustomers}" assignTo="{!contNonCustomers}"/>
<apex:param name="contOpenCoverages" value="{!ar.NumOpenCoverages}" assignTo="{!contOpenCoverages}"/>
<apex:param name="contName" value="{!ar.OwnerName}" assignTo="{!contName}"/>
<apex:param value="All" assignTo="{!contType}" name="contType" />
<apex:param value="{!ar.RepSelection}" name="contRepSelection" assignTo="{!contRepSelection}" />
<apex:param value="{!ar.Zip}" name="contZip" assignTo="{!contZip}" />
</apex:commandButton>

</apex:column>
 </apex:pageBlockTable>
</apex:pageBlockSection>

 

//then, on my controller, the variable contrepselection contain the value of the rep that the user selected.

public contrepselection{get;set} is how i declared it.

 

All Answers

SaintMichaelSaintMichael

Without looking at your code, that usually happens when you fail to loop.

Are you actually looping?

 

Are you using a structure, like a Map, that will reject dupes?

 

 

claperclaper

I'm not using structures nor maps; and i am looping to get the values to the picklist, and also using a wrapper class called 'AccByZip' to wrap all the information from rows.

 

thanks for the prompt reply!!

claperclaper

And here comes the solution!! I decided to post the code for others that might face the same issue. The trick was to rerender the pageblocktable once the user pick a value from the dropdown list. here is the code, high lighted in red.

 

VF PAGE:

<apex:pageBlockSection title="Establishments by Zip" id="SecondSection" columns="1" >
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockTable value="{!AccountsByZip}" var="ar" id="accountsbyzip" >
<apex:column >
<apex:facet name="header">Zip</apex:facet>
<apex:outputText value="{!ar.Zip}" />
</apex:column>
<apex:column >
<apex:facet name="header">Estab Rep</apex:facet>
<apex:outputText value="{!ar.ownerName}" >
</apex:outputText>
</apex:column>
<apex:column >
<apex:facet name="header"># of Establishments</apex:facet>
<apex:outputText value="{!ar.NumAccounts}" />
</apex:column> 
<apex:column >
<apex:facet name="header"># of Customers</apex:facet>
<apex:outputText value="{!ar.NumCustomers}" />
<apex:outputText value="0" rendered="{!if(ar.NumCustomers > 0 , false, true)}"/>
</apex:column>
<apex:column >
<apex:facet name="header"># of Non Customers</apex:facet>
<apex:outputText value="{!ar.NumNonCustomers}" />
<apex:outputText value="0" rendered="{!if(ar.NumNonCustomers > 0 , false, true)}"/>
</apex:column>
<apex:column >
<apex:facet name="header"># of Open Coverage</apex:facet>
<apex:outputText value="{!ar.NumOpenCoverages}" />
</apex:column>

//this is where the magic happens. {!RepOptions} contains all the values of the rep. then I use param tag to pass the value to a variable called contRepSelection on my controller, when the user changes the value of the picklist
<apex:column >
<apex:facet name="header">Reassign</apex:facet>
<apex:selectList value="{!ar.RepSelection}" size="1" id="repvalue" >
<apex:selectOptions value="{!RepOptions}"/>
<apex:actionSupport event="onchange" reRender="accountsbyzip" >
<apex:param value="{!ar.RepSelection}" name="contRepSelection" assignTo="{!contRepSelection}" />
</apex:actionSupport>
</apex:selectList>
</apex:column>

//This is the button that the user presses after changing the rep picklist, this button its located on the same row of the pageblocktable as the picklist. when the user press the button i used param value, to pass all the columns data for that row into the controller. NOTICE THAT I PASS AGAIN THE VALUE OF THE ar.Repselection TO THE contRepselection VARIABLE.  This is how i got it to work for me. 
<apex:column >
<apex:facet name="header">Preview</apex:facet>
<apex:commandButton value="Preview" style="width: 100px;" action="{!dofetchAccounts}" rerender="accounts, accountsbyzip" status="loading" id="btnpreview" >
<apex:param name="contCustomers" value="{!ar.NumCustomers}" assignTo="{!contCustomers}"/>
<apex:param name="contNonCustomers" value="{!ar.NumNonCustomers}" assignTo="{!contNonCustomers}"/>
<apex:param name="contOpenCoverages" value="{!ar.NumOpenCoverages}" assignTo="{!contOpenCoverages}"/>
<apex:param name="contName" value="{!ar.OwnerName}" assignTo="{!contName}"/>
<apex:param value="All" assignTo="{!contType}" name="contType" />
<apex:param value="{!ar.RepSelection}" name="contRepSelection" assignTo="{!contRepSelection}" />
<apex:param value="{!ar.Zip}" name="contZip" assignTo="{!contZip}" />
</apex:commandButton>

</apex:column>
 </apex:pageBlockTable>
</apex:pageBlockSection>

 

//then, on my controller, the variable contrepselection contain the value of the rep that the user selected.

public contrepselection{get;set} is how i declared it.

 

This was selected as the best answer
AKHIL VARSHNEY 14AKHIL VARSHNEY 14
@Claper, how u did it , i am facing the challenge as the first records selected value from picklist is not gettong passed tp controller but 2nd record is getting passed. You mentioned u done but your clarification is not working fine.