0

Pagination is not working while clicking on next button

Apex Code:

/* Date:21/01/2019 Auther:SudhanshuDescription:Account Search input box   * /

public class AccountEmailAlert {
    public Integer rec { get; set; }  
    public Integer tot { get; set; }
    integer count= 0;
    integer totalRecs = 0;
    integer LimitSize= 5;

    public List<wrapAccount> wrapAccountList {get; set;}
    public list <Account> acc {get;set;}
    public String searchKey {get;set;}
    Set<Id> accids = new Set<Id>();
    public AccountEmailAlert( ) {
        tot=totalRecs = [select count() from account];
    }
    public void search(){
        wrapAccountList = new List<wrapAccount>();
        if(searchKey!=null && searchKey!='' && wrapAccountList !=null)
        {
            string searchquery='select Name,id,Type,Rating from account where name like \'%'+searchKey+ '%\'LIMIT:limitsize OFFSET:count';
            acc= Database.query(searchquery); 
            for (Account ac : acc){
                wrapAccountList.add(new wrapAccount(ac));
            }

        }   
        else {
            ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Can not be empty textbox' );
            ApexPages.addmessage(msg);
            clear();  
        }       
    }
    public void clear(){
        wrapAccountList.clear();
    }
    public void sendEmail()
    {   
        if(wrapAccountList != null){
            processSelected();
            if(accids.isEmpty()){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please select the checkbox'));
            }
            List<Contact> conList=[SELECT Id, Name, Email, AccountId FROM Contact where Accountid IN:accids ];
            system.debug('error'+conList);
            EmailTemplate templateId = [Select id from EmailTemplate where name = 'Contact Email Templates'];
            List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
            for(contact con : conList)
            {            
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTemplateID(templateId.Id); 
                mail.setTargetObjectId(con.id);
                mail.setReplyTo(con.Email);
                mail.setSaveAsActivity(false);
                allmsg.add(mail);
            }

            for(wrapAccount wa:wrapAccountList){
                Integer counter= allmsg.size();
                if(counter>0 && wa.selected==true){
                    Messaging.sendEmail(allmsg,false);
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,counter+' ' +'E-mail sent successfuly to related contacts')); 

                    if(counter>0 && wa.selected==false){
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No records selected from checkbox')); 
                    }
                }

            }
        }

        else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'You can not send email without fetching records'));
        }
    }

    public void processSelected() {
        acc = new List<Account>();      
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {              
                acc.add(wrapAccountObj.acc1);                  
            } 
        }    
        for(Account aa:acc)
        {
            accids.add(aa.id);
        }
    }
    public class wrapAccount {
        public Account acc1 {get; set;}
        public Boolean selected {get; set;}
        public wrapAccount(Account a) {
            acc1 = a;
            selected = false;
        }       
    }

    public PageReference Firstbtn() {
        count=0;
        return null;
    }
    public PageReference prvbtn() {
        count=count-limitsize;
        return null;
    }
    public PageReference Nxtbtn() {
        count=count+limitsize;
        return null;
    }
    public PageReference lastbtn() {
        count= totalrecs - math.mod(totalRecs,LimitSize);
        return null;
    }
    public Boolean getNxt() {
        if((count+ LimitSize) > totalRecs)
            return true;
        else
            return false;     
    }
    public Boolean getPrv() {
        if(count== 0)
            return true;
        else
            return false;
    }  
}

Visualforce Code:

<apex:page controller="AccountEmailAlert" showHeader="false" sidebar="false">
    <script type="text/javascript">
    function selectAllCheckboxes(obj,receivedInputID){
        var inputCheckBox = document.getElementsByTagName("input");
        for(var i=0; i<inputCheckBox.length; i++){
            if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                inputCheckBox[i].checked = obj.checked;
            }
        }
    }
    </script>

    <apex:form >
        <apex:pageblock >
            <apex:pageMessages id="msg"></apex:pageMessages>
            <apex:pageBlockSection title="Account Search box">
                <apex:outputLabel value="Name"></apex:outputLabel>
                <apex:inputText value="{!searchKey}" />

                <apex:outputPanel style="float:centre">
                    <apex:commandButton value="Search records" action="{!search}" />

                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageblock>
        <apex:pageBlock title="Search Result">          
            <apex:pageBlockTable value="{!wrapAccountList}" var="a">
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!a.selected}" id="inputId"/>
                </apex:column>
                <apex:column value="{!a.acc1.name}"/>
                <apex:column value="{!a.acc1.rating}"/>
                <apex:column value="{!a.acc1.type}"/>

                <apex:column value="{!a.acc1.id}"/>
            </apex:pageBlockTable>
            <div align="center">
                <apex:pageBlockButtons >
                     <apex:commandButton value="send" action="{!sendEmail}"/>
                </apex:pageBlockButtons>                
            </div>
            <div align="right">
            <apex:commandButton value="First Page" action="{!Firstbtn}" disabled="{!prv}" reRender="pg1"/>
          <apex:commandButton value="PreviousPage" action="{!prvbtn}" disabled="{!prv}" reRender="pg1"/>
         <apex:commandButton value="NextPage" action="{!Nxtbtn}" disabled="{!nxt}" reRender="pg1"/>
       <apex:commandButton value="LastPage" action="{!lastbtn}" disabled="{!nxt}" reRender="pg1"/>
         </div>

            <apex:outputLabel >Total no of recs:{!tot}</apex:outputLabel>


        </apex:pageBlock>
    </apex:form>
</apex:page>
2
  • You will have to re-render PageBlockTable on click of CommandButtons. Currently, it is re-rendering "pg1" which is not present in your page. Set the Id attribute of PageBlockTable to "pg1" and see if it works. Commented Jan 31, 2019 at 12:03
  • Hi @Vijay Ganji. I have given id as you suggested but it is not moving further if i click on next button and no records coming further. what can i do now?? Commented Jan 31, 2019 at 12:22

1 Answer 1

1

You will have to make few changes for this to work

  • Set the Id of PageBlockTable as "pg1"as you are re-rendering "pg1" from CommandButtons
  • You will have to call the search method in all of your action methods which are getting called from CommandButtons

So you will have to make below change in your visualforce page

<apex:pageBlockTable id="pg1" value="{!wrapAccountList}" var="a">

And below changes in your controller

public void Firstbtn() {
    count=0;
    search();
}
public void prvbtn() {
    count=count-limitsize;
    search();
}
public void Nxtbtn() {
    count=count+limitsize;
    search();
}
public void lastbtn() {
    count= totalrecs - math.mod(totalRecs,LimitSize);
    search();
}
1
  • P.S. you can change return type to void and remove the return lines.
    – sfdcfox
    Commented Jan 31, 2019 at 15:12

You must log in to answer this question.

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