Visualforce Pagination with Dynamic Search

This post has a working example of dynamic search in a Visualforce page and the results shown with pagination.

There are 4 field search criteria including date fields too which makes it more interesting.  The results upon clicking Search will display below the Search section with 4 buttons to paginate.  The Next/Last Page buttons grey out when there are no further records in the results.

APB

Apex Controller

Visualforce Page

Here’s the complete github code

Advertisements

32 Replies to “Visualforce Pagination with Dynamic Search”

  1. Hi

    Are there any prerequisites to make this code execute?? Its showing “unexpected token: ‘accountList’ at line 3 column 13”

    Also “Could not resolve the entity from value binding ‘{!acc.Created_From_Date__c}’. can only be used with SObjects, or objects that are Visualforce field component resolvable” though i have created two custom fields by same name.

    Please help me out

    Like

        1. Hi
          Am here again.. actually i have written a test class for the same but its covering only 73%

          also if i call previous method its throwing error. Please help me out

          @isTest
          public class ControllerTestClass
          {
          static testMethod void testMethod1()
          {
          Account testAccount = new Account();
          testAccount.Name=’Test Account’ ;
          testAccount.FromDate__c = Date.newInstance(2016, 12, 9);
          insert testAccount;

          Test.StartTest();

          PageReference pageRef = Page.DateAccount; // Add your VF page Name here
          pageRef.getParameters().put(‘id’, String.valueOf(testAccount.Id));
          Test.setCurrentPage(pageRef);

          AccountMultipleSearchWithPagenationCLS1 testAccPlan = new AccountMultipleSearchWithPagenationCLS1();

          testAccPlan.searchAcc();
          //testAccPlan.searchAccounts();
          testAccPlan.FirstPage();
          testAccPlan.next();
          testAccPlan.LastPage();
          testAccPlan.getprev();
          testAccPlan.getnxt();
          //testAccPlan.previous();
          Test.StopTest();
          }
          }

          Like

          1. @Ishwarya: In the test class, can you try to insert more than 10 Account records and then try. Because the page size is 10, the method “Previous” would work fine if the there are at least 11 Account records.

            Like

                1. Hi, did you populate the 2 fields Created_To_Date__c and Created_From_Date__c in all the Accounts you created in the test class? If those are populated, the date loops should be covered.

                  Like

                    1. You can add this line

                      testAccPlan.acc = testAccount;

                      below the line AccountMultipleSearchWithPagenationCLS1 testAccPlan = new AccountMultipleSearchWithPagenationCLS1();

                      Like

                    2. To populate the dates, use the below lines for the account

                      testAccount.Created_From_Date__c= Date.newInstance(2016, 12, 9);
                      testAccount.Created_To_Date__c= Date.newInstance(2016, 12, 10);

                      Like

                    3. Hi

                      I have followed ur comments and written below. But though its covering dates when i give previous throwing error

                      @isTest
                      public class ControllerTestClass
                      {
                      static testMethod void testMethod1()
                      {
                      Account testAccount = new Account();
                      testAccount.Name=’Test Account’ ;
                      testAccount.FromDate__c= Date.newInstance(2016, 12, 9);
                      testAccount.ToDate__c= Date.newInstance(2016, 12, 10);
                      insert testAccount;
                      Account testAccount1 = new Account(Name = ‘ttt’);
                      testAccount1.ToDate__c = Date.newInstance(2016, 12, 18);
                      insert testAccount1;
                      Account testAccount2 = new Account(Name = ‘yyy’);
                      insert testAccount2;
                      Account testAccount3 = new Account(Name = ‘q’);
                      insert testAccount3;
                      Account testAccount4 = new Account(Name = ‘c’);
                      insert testAccount4;
                      Account testAccount5 = new Account(Name = ‘yt’);
                      insert testAccount5;
                      Account testAccount6 = new Account(Name = ‘ii’);
                      insert testAccount6;
                      Account testAccount7 = new Account(Name = ‘ui’);
                      insert testAccount7;
                      Account testAccount8 = new Account(Name = ‘yui’);
                      insert testAccount8;
                      Account testAccount9 = new Account(Name = ‘nj’);
                      insert testAccount9;
                      Account testAccount10 = new Account(Name = ‘lo’);
                      insert testAccount10;
                      Test.StartTest();

                      PageReference pageRef = Page.DateAccount;
                      pageRef.getParameters().put(‘id’, String.valueOf(testAccount.Id));
                      Test.setCurrentPage(pageRef);

                      AccountMultipleSearchWithPagenationCLS1 testAccPlan = new AccountMultipleSearchWithPagenationCLS1();
                      testAccPlan.acc = testAccount;
                      testAccPlan.searchAcc();
                      testAccPlan.searchAccounts();
                      testAccPlan.FirstPage();
                      testAccPlan.next();
                      testAccPlan.LastPage();
                      testAccPlan.getprev();
                      testAccPlan.getnxt();
                      testAccPlan.previous();
                      Test.StopTest();
                      }

                      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s