Search Contact Records Using Pagination in apex

Requirement : Create a visual force page create a TextBox for Last Name and another is Mobile Phone when User enter the value then corresponding data should be visible in a table

Solution:

VFPageCode

<apex:page controller="ContactMultipleSearchWithPagenationCLS" action="{!searchcon}">
     <script type="text/javascript">
        window.onload=function() {
        // document.getElementById("{!$Component.thePb.thepbs.conName}").focus();
        }   
    </script>
     <apex:form >
        <apex:pageBlock id="thePb" title="Contact Details To Search">
            <apex:pageblockSection id="thepbs">

                <apex:inputField value="{!con.LastName}" required="false" id="conName"/>
                <apex:inputfield value="{!con.MobilePhone}"/>
            </apex:pageblockSection>
            
            <apex:pageblockButtons location="bottom">
                <apex:commandButton value="Search" action="{!searchcon}" />
            </apex:pageblockButtons> 
        
        </apex:pageBlock>

        <apex:pageBlock title="Contact Details" id="noRec" rendered="{! IF( contactList != null && contactList.size ==0 , true, false)}" >
            <apex:outputPanel >
                <h1>No Records Found </h1>
            </apex:outputPanel>
        </apex:pageBlock>


        <apex:pageBlock title="Contact Details" id="details" rendered="{! IF( contactList != null && contactList.size >0, true, false)}" >

            <apex:pageBlockTable value="{!contactList}" var="c">
                <apex:column headerValue="conount Name">
                <apex:outputLink target="_blank" value="/{!c.id}">{!c.LastName}</apex:outputLink> 
                </apex:column>   
                <!--  If you want facet style you can add like this.
                <apex:column >
                <apex:facet name="header">Link Name</apex:facet>
                <apex:outputLink target="_blank" value="/{!c.id}">{!c.LastName}</apex:outputLink> 
                </apex:column>
                -->
                <apex:column value="{!c.MobilePhone}" headerValue="Mobile Phone"/>  
                <apex:column value="{!c.Email}" headerValue="Email"/>  

            </apex:pageBlockTable>

            <apex:pageblockButtons >
                <apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
                <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
                <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
                <apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>
            </apex:pageblockButtons>

        </apex:pageBlock>

     </apex:form>
</apex:page>


ControllerCode

public with sharing class ContactMultipleSearchWithPagenationCLS {
    public Contact con{get;set;}
    public List<Contact> ContactList {get;set;}
    // create a list of strings to hold the conditions
    List<string> conditions = new List<string>();
    private integer totalRecs = 0;
    private integer OffsetSize = 0;
    private integer LimitSize= 10;

    public ContactMultipleSearchWithPagenationCLS(){
    system.debug('==>ContactMultipleSearchWithPagenationCLS  is calling==>');
     con = new Contact();
     //ContactList  = new List<Contact>();
    }

    public void searchcon(){
        totalRecs = 0;
        OffsetSize = 0;
        if(ContactList !=null && ContactList.size()>0){
            ContactList=null;
        }
        
        searchContacts ();
        conditions.clear();
    }


    public Void searchContacts(){

        System.debug('Total Records is ==>'+totalRecs);
        System.debug('OffsetSize is ==>'+OffsetSize);

        if(ContactList != null && !ContactList.isEmpty()){
          ContactList.clear();
        }
         String strQuery ='SELECT Id,Name,LastName,MobilePhone,Email From Contact';
         


        if(con.LastName !=null && con.LastName !=''){
            conditions.add('LastName Like \'%' +con.LastName +'%\' ');
        }
        
        if(con.MobilePhone !=null && con.MobilePhone !=''){
            conditions.add('MobilePhone Like\'%' +con.MobilePhone +'%\' ');
        }

        if (conditions.size() > 0) {
            strQuery += '  WHERE ' + conditions[0];
            for (Integer i = 1; i < conditions.size(); i++)
                    strQuery += '  AND ' + conditions[i];
        }
        
        if(totalRecs !=null && totalRecs ==0){
            List<Contact> conTemp = Database.query(strQuery);
            totalRecs = (conTemp !=null &&conTemp.size()>0)?conTemp.size():0;
        }

        system.debug('strQuery ==>'+strQuery );
        // add sort and limits at the end  
        strQuery += ' ORDER BY Name  ASC, CreatedDate DESC LIMIT :LimitSize OFFSET :OffsetSize';

        ContactList  =Database.query(strQuery);
          
           

          //conditions.clear();
          //return ContactList.size();
    }

    public void FirstPage()
    {
        OffsetSize = 0;
        searchContacts();
    }
    public void previous()
    {
        OffsetSize = (OffsetSize-LimitSize);
        searchContacts();
    }
    public void next()
    {
        OffsetSize = OffsetSize + LimitSize;
        searchContacts();
    }
    public void LastPage()
    {
        OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
        searchContacts();
    }
    public boolean getprev()
    {

        if(OffsetSize == 0){

            return true;
        }
        else {

            return false;
        }
    }
    public boolean getnxt()
    {
        if((OffsetSize + LimitSize) > totalRecs){

            return true;
        }
        else {

            return false;
        }
    }


}


OutPut:





Thanks,
Sumit Shukla
skypeId:shuklasumit1
email: sumitshukla.mca@gmail.com



Comments

Popular Post

Write The Dynamic Query in Salesforce and bind in page block table with filter conditions

Fetch Picklist value in LWC using Schema class

Custom Section Color at Visual force page