-1

This question already has an answer here:

    public class AccountSearchController1 {   //main class
        //results variables
    public Boolean hasResults{get; set;}
    public List<accountSet> resultSet {get;set;}
    public List<accountSet> outputSet{get;set;}
     //Input Fields variables
    public String countryField{get;set;}
    public String contactNameField{get;set;}
    public Account accField{get;set;}
    public Account_Search__c accountSearchSet{get; set;}
     //Pagination Variables
    public Boolean hasPrevious{get;set;}
    public Boolean hasNext{get;set;}
    public Integer PageNumber{get;set;}
    public Integer ResultSize{get;set;}
    private Integer pageSize;
     // ** Search Settings Variables ** //
    public Account_Search__c setting {get; set;}

    public AccountSearchController1() { // main class constructor
        // ** Input Variables Intialization ** //
        accField = new Account();
        // ** Result Variables Intialization ** //            
        resultSet = new List<accountSet>();
        outputSet= new List<accountSet>();
        hasResults = false;
        ResultSize = 0;
        accountSearchSet = Account_Search__c.getInstance(UserInfo.getUserId());

        // **  Pagination Variables Intialization ** //        
        hasPrevious = false;
        hasNext = false;
        PageNumber = 1;

        // ** Search Settings Variables Intialization ** //
        setting = [SELECT  Enable_Account_Type__c, Enable_Country__c, Enable_Contact_Name__c, Page_Size__c FROM Account_Search__c WHERE Label__c = 'Default'];
        pagesize = (Integer) setting.Page_Size__c;
    }

    public class accountSet{      //inner class/wrapper class
        public Account acc{get;set;}   //inner class variables
        public Contact con{get;set;}

        public accountSet(Account ac, Contact cn){ //inner class constructor to account with contact
            this.acc = ac;
            this.con = cn;
        }
        public accountSet(Account ac){ //inner class constructor to account without contact
            this.acc = ac;
            this.con = null;
        }
    }

    public List<Contact> findAccounts(){ //find accounts method
        //** CLEAR LIST FOR NEW QUERY **//
        resultSet.clear();
        //** DYNAMIC QUERY STRING **//
        String queryContactName = '';
        List<String> filters = new List<String>();
        String accName;

        // ** WILDCARD INTERACTIONS  AND FILTERS ** //
        Boolean conName_asterisk = false;
        Boolean country_asterisk = false;

        if(! String.isEmpty(contactNameField)){
            if(contactNameField.contains('*')){
                contactNameField = contactNameField.remove('*');
                conName_asterisk = true;
            }
            queryContactName = 'WHERE Contact.Name LIKE \'%' + contactNameField.trim() + '%\'';
        }
        if(accField.name != null){
            if(accField.name.contains('*')){
                accField.name = accField.name.replace('*', '%');
            }
            filters.add('Account.Name LIKE \'' + accField.name.trim()  + '\'');
        }
        if(! String.isEmpty(countryField)){
            if(countryField.contains('*')){
                countryField= countryField.remove('*');
                country_asterisk = true;
            }
            filters.add('Account.BillingCountry LIKE \'%' + countryField.trim() + '%\'');
        }
        if(accField.Type != null){
            filters.add('Account.type = \''+ accField.Type + '\'');
        }

        // ** VALIDATIONS ** //
        if(String.isEmpty(accField.name) && String.isEmpty(countryField) && String.isEmpty(contactNameField) && accField.type == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,Label.Account_Search_No_Filter_Error));
            hasResults = false;
            return null;
        }

        if(accField.name!=null && accField.name.length()<2){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,Label.Account_Name_Search_Error));
            hasResults = false;
            return null;
        }      

        // ** QUERY BUILDING ** //

        String query = 'SELECT Account.Id, Account.Name, Account.BillingCountry, Account.ShippingCountry, '+
            '(SELECT Name, CreatedDate FROM Contacts ' + queryContactName + '), ' +
            'Account.Type, Account.CreatedDate FROM Account ';

        if(filters.size()>0){
            query += 'WHERE ' + String.join(filters, ' AND ');
        }
        System.debug('QUERY > '+query);
        List<Account> acList = Database.query(query+' ORDER BY Account.Name ASC');

        // ** CREATION OF WRAPPER CLASS ** //
        for(Account acc: acList){
            if(acc.contacts.size()>0){
                for(Contact con: acc.contacts){
                    resultSet.add(new accountSet(acc, con));
                }
            }
            else if(String.isEmpty(contactNameField) || acc.contacts.size() > 0)
                resultSet.add(new accountSet(acc));         

        }
        hasResults = resultSet.size() > 0 ? true : false;
        ResultSize = resultSet.size();
        fillOutputSet();
        return null;         
    }

    private void fillOutputSet(){
        if(! outputSet.isEmpty())
            outputSet.clear();

        Integer startIndex = (Integer)setting.Page_Size__c*(PageNumber-1);
        for(Integer i = 0; i < setting.Page_Size__c && (startIndex+i)<resultSize; i++){
            outputSet.add(resultSet.get(startIndex + i));
        }

        hasPrevious = PageNumber > 1 ? true : false;
        hasNext = PageNumber <  Math.ceil(ResultSize / setting.Page_Size__c)  ? true: false;
    }

    public void onchangePageSize(){
        PageNumber = 1;
        fillOutputSet();
    }

    public void previous(){
        pageNumber--;
        fillOutputSet();        
    }

    public void next(){
        pageNumber++;
        fillOutputSet();
    }
    public void first(){
        PageNumber = 1;
        fillOutputSet();
    }
    public void last(){
        PageNumber = (Integer)Math.ceil(ResultSize / setting.Page_Size__c);
        fillOutputSet();
    } 
}

marked as duplicate by Sebastian Kessel, Jayant Das, David Reed apex Jan 22 at 3:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.