1

I need to be able to edit and save records in a pagination. Right now I can get the pagination to work if i have apex:pageBlockTable value="{!Accounts}" but then i can't refresh the page with a different picklist selection or search accounts. Alternatively if i set apex:pageBlockTable value="{!RecordList}" i can search accounts and switch accounts with the dropdown menu but pagination doesn't work.

*********************************************SOLVED*******************************************************

UPDATED CODE BELOW

APEX

public class AccountTypeController 
{
    //declare variables

    public Account filterAccount{get; set;}
     List<Account> accList = new List<Account>();
    //search
    public String searchString { get; set; }
    public Account[] accs{ get; set; }
    //Return table
    public Account[] recordList{get; set;}
    public Account[] updateList{get; set;}
    //Pagination
    public Integer noOfRecords{get; set;}
    public Integer pageSize{get;set;}
    public List<SelectOption> paginationSizeOptions{get;set;}
    public ApexPages.StandardSetController setCon {get; set;}


    //constructor
    public AccountTypeController()
    {
        //initialize variables
        filterAccount = new Account();
        recordList = new List<Account>();
        pageSize=20;
        Database.QueryLocator qLoc;

        //initialize set controller using a SOQL query:
        qLoc = Database.getQueryLocator([Select Name, Type, Website, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry  
                    from Account where type =:filterAccount.type Order by Name ASC Limit 10000]);
        setCon = new ApexPages.StandardSetController(qLoc);

        paginationSizeOptions = new List<SelectOption>();
        paginationSizeOptions.add(new SelectOption('5','5'));
        paginationSizeOptions.add(new SelectOption('10','10'));
        paginationSizeOptions.add(new SelectOption('20','20'));
        paginationSizeOptions.add(new SelectOption('50','50'));
        paginationSizeOptions.add(new SelectOption('100','100'));

    }

   public void doSearch() {
        String soqlQuery = 'SELECT id, Name, type, website, BillingStreet, BillingCity, BillingState,'+ 
                    'BillingPostalcode, BillingCountry FROM Account';
        String tempTerm;
        if(String.isNotBlank(searchString)) {
            tempTerm = '%'+searchString+'%';
            soqlQuery += ' WHERE Name LIKE :tempTerm';
        }
        recordList = Database.query(soqlQuery);
   }

    //saving records
    public pageReference save()
    {
        try
        {
            update recordList;
        }
        catch(Exception e)
        {
            System.debug('Exception occurred ' + String.valueOf(e));

        }    
        return null;
    }



    // Initialize setCon and return a list of record    

    public List<Account> getAccounts() 
    {
                return (List<Account>) setCon.getRecords();
    }

       //fields returning for table
  public void updateRecordList()
    {
        setCon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name, Type, Website, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry  
                    from Account where type =:filterAccount.type Order by Name ASC Limit 10000]));
    }



     //Changes the size of pagination
    public PageReference refreshPageSize() {
         setCon.setPageSize(pageSize);
         return null;
    }

     public Integer resultSize
    {
        get{
                return noOfRecords;
        }
        set;
    }

    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }

    public pageReference setpageSize()
    {
      setCon.setPageSize(PageSize);
        return null;
    }

        public void first() {
        setCon.first();
    }

    public void last() {
        setCon.last();
    } 
    public void previous() {
        setCon.previous();
    }

    public void next() {
        setCon.next();
    }



}

VF

<apex:page controller="AccountTypeController" sidebar="false">

    <apex:form ><br/><br/>

    <!---Accounts by Type title-->
      <div style="text-align:center; font-size:30px;">
        <apex:outputText value="Accounts By Type"/>
      </div>

      <apex:actionFunction name="refreshPageSize" action="{!refreshPageSize}" status="fetchStatus" rerender="account_filter"/>   

        <apex:outputPanel style="display: block" id="pwPanel">

          <apex:pageBlock >

                <apex:inputText value="{!SearchString}" id="theSearchString" maxLength="100" size="30"/>
                 <apex:commandButton value="Search" action="{!doSearch}" rerender="pwPanel"/>
               <br/>

               <apex:inputfield value="{!filterAccount.Type}" label="Account Type"> 
                 <apex:actionsupport event="onchange" action="{!updateRecordList}" rerender="pwPanel"/>                  
               </apex:inputfield>

   <br/><br/><br/>


            <!-----------Table---------->                  

               <apex:outputPanel rendered="{!Accounts.size > 0}" id="account_filter">

                   <apex:pageBlockSection >


                    <apex:pageBlockTable value="{!Accounts}" var="rl" id="record_list">

                       <apex:inlineEditSupport showOnEdit="saveButton"
                                               hideOnEdit="editButton" event="onclick"/>

                            <apex:column headerValue="Account Name" value="{!rl.Name}">           
                            </apex:column>

                            <apex:column headerValue="Type">
                                <apex:outputField value="{!rl.Type}"/>
                            </apex:column>

                            <apex:column headerValue="Website">
                                <apex:outputField value="{!rl.Website}"/>
                            </apex:column>

                            <apex:column headerValue="Billing Street">
                                <apex:outputField value="{!rl.BillingStreet}"/>
                            </apex:column>

                            <apex:column headerValue="Billing City">
                                <apex:outputField value="{!rl.BillingCity}"/>
                            </apex:column>

                            <apex:column headerValue="Billing State">
                                <apex:outputField value="{!rl.BillingState}"/>
                            </apex:column>

                            <apex:column headerValue="Billing Zip/Postal Code">
                                <apex:outputField value="{!rl.BillingPostalCode}"/>
                            </apex:column>

                            <apex:column headerValue="Billing Country">
                                <apex:outputField value="{!rl.BillingCountry}" />
                            </apex:column>

                    </apex:pageBlockTable> 

                   <apex:commandButton action="{!save}" id="SaveButton" value="Save"/>
                   </apex:pageBlockSection>



<!-----------First, Previous, Next, Last Command Buttons---------->                      

                <apex:panelGrid columns="8"> 

                <apex:selectList value="{!pagesize}" multiselect="false" size="1" onchange="refreshPageSize();">
                    <apex:selectOptions value="{!paginationSizeOptions}"/>
                </apex:selectList>


                <apex:commandButton status="fetchStatus" reRender="account_filter" value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/> 

                <apex:commandButton status="fetchStatus" reRender="account_filter" value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/> 

                <apex:commandButton status="fetchStatus" reRender="account_filter" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/> 

                <apex:commandButton status="fetchStatus" reRender="account_filter" value="Last" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/> 


                <apex:outputPanel >                      
                    <apex:actionStatus id="fetchStatus" >
                        <apex:facet name="start" >
                          <img src="/img/loading.gif" />                    
                        </apex:facet>
                    </apex:actionStatus>
                </apex:outputPanel> 

            </apex:panelGrid> 



<!---------------------------------------------------->

                </apex:outputPanel>
            </apex:pageBlock>  

         </apex:outputPanel>

    </apex:form>

</apex:page>
4
  • 1
    you should check out this blog post for an elegant way to do standardsetcontrollers that uses less code than you have. Note how the pagesize is done via onChange event on the selectList
    – cropredy
    Jul 25, 2019 at 23:05
  • That really cleaned up a lot of the code but pageSize still isn't working correctly nor is the next,previous,last,first. It still only displays one page.
    – Tyler
    Jul 26, 2019 at 2:23
  • i literally just implemented the code in sfdcpoint.com/salesforce/… and it worked perfectly first time
    – cropredy
    Jul 26, 2019 at 18:48
  • Yeah maybe the code in the blog because its not doing as much as mine. Mine works until you try to select a different picklist option and then it wont update. I need to be able to edit and save the records.
    – Tyler
    Jul 26, 2019 at 18:52

1 Answer 1

2

Visualforce does not automatically rerender changed attributes. To reflect changed attributes, you need to rerender the outputpanel inside which you know there are changes or else use getters.

You should be using below in apex code:

public List<Opportunity> getRecordlist() {
     return (List<Account>) setCon.getRecords();
}

This is a getter which will update the records when you use RecordsList as value in table.

Also use outputpanel around table and rerender that:

<apex:commandButton reRender="pwPanel" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/> 

Also move pwPanel just around table or else inputField will also get rerendered which is not needed.

Good practices:

StandardSetController already has methods like getHasNext() and next() or last(). You dont need to implement these methods in apex. Lot of visualforce developers are unaware of this.

Refer: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardSetController_methods.htm

example:

<apex:commandButton reRender="tabelPanel" value="Last" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>

--- added ----

Also updateRecordList is just assigning to recordList but not returning set controller. you can remove updateRecordList and You need to implement:

public ApexPages.StandardSetController setCon {
    get {
        if(setCon == null) {                
            setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                  [Select Name, Type, Website, BillingStreet, BillingCity, BillingState, 
                BillingPostalcode, BillingCountry 
                From Account Where Type = :filterAccount.Type
                Limit 10000]));
            setCon.setPageSize(size);  
            noOfRecords = setCon.getResultSize();
        }            
        return setCon;
    }
    set;
}

----added-----

public ApexPages.StandardSetController setcon {
    get{
        if(setCon == null){
            setCon = new ApexPages.StandardSetController(Database.getQueryLocator(recordsList()));
            setCon.setpageSize(pageSize);
            noOfRecords = setCon.getResultSize();
        }
        return setCon;
    }set;
}

void recordsList(){
    return [Select Name, Type, Website, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry  
                from Account where type =:filterAccount.type Order by Name ASC Limit 10000];
}

public void updateRecordList()
{
    setcon=null;
}
9
  • added answer... Jul 26, 2019 at 4:06
  • Im using updaterecordlist to be able to save the records after editing. I also already have that code implemented.
    – Tyler
    Jul 26, 2019 at 13:52
  • Pls mark as solved if solved Jul 26, 2019 at 14:41
  • you doing rerender of pwPanel there Jul 26, 2019 at 15:39
  • you can actually use refreshPageSize() for onchange so that it rerenders page block Jul 26, 2019 at 15:41

Not the answer you're looking for? Browse other questions tagged .