function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harrison LauHarrison Lau 

Help writing test Class for Dynamic VF Page and Controller

Hi friends,
I am an admin making the transition to developer so forgive me if this is/should be considered common knowledge but...
I was wondering if someone would be able to help me write a test class for this Dynamic Search Page controller.  I understand in theory how to write a test class but I'm not sure i would be able to successfully pull it off on my own.  

Any help would be MUCH appreciated!
 
public with sharing class WrapperClass{
    
    //Instantiate Lists that we will be using
    public List<wrapAccount> wrapCTAList {get;set;}
    public List<CTA_Database__c> selectedCTA{get;set;}
    public List<wrapAccount> tempWrapCTAList{get;set;}
    
    //empty Constructor
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        
    } 
    
    //Instantiate lists and runQuery when page loads
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        
        soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c, Checkbox__c, Family_Status__c FROM CTA_Database__c WHERE Id != null';
        runQuery();
    }
    
    //Grab records from wrapCTAList where 'selected' property is true, then set checkbox__c value true/false based on 'selected'
    public void processSelected() {        
        List<wrapAccount> tempWrapCTAList = new List<wrapAccount>();
        //for each record on QueryList        
        for(wrapAccount objWrap: wrapCTAList) {
            //if selected, then checkbox is true            
            if(objWrap.selected == true) {
                objWrap.acc.checkbox__c = true;
                //update checkbox field in database
                update (objWrap.acc);
            } else {
                //if not selected, then checkbox is false
                if (objWrap.selected != true) {
                    objWrap.acc.checkbox__c = false;
                    //update checkbox field in database
                    update (objWrap.acc);
                }
            }
            //Check to see if there are already 'checkboxed records', set their selected property to true
            if (objWrap.acc.checkbox__c == true) {
                string selectedName = objWrap.acc.name;
                for (wrapAccount qWrap: wrapCTAList) {
                    if (qWrap.acc.name == selectedName) {
                        qWrap.selected = true;
                        system.debug(selectedName);
                    }
                }
            } 
        }
        //add records to Results Table
        buildSelections();
    }
    
    //add values to Results Table
    public void buildSelections(){
        tempWrapCTAList = new List <wrapAccount>();       
        string selectSoql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c, Checkbox__c, Family_Status__c FROM CTA_Database__c WHERE checkbox__c = true'; 
        
        //for each record where checkbox__c is true, add to results list.
        for(CTA_Database__c obj : Database.query(selectSoql + ' order by ' + sortField + ' ' + sortDir)) {
            tempwrapCTAList.add(new wrapAccount(obj));
        }        
    }
    
    //SELECT all records in QueryList
    public PageReference selectAll() {
        for(wrapAccount q: WrapCTAList) {
            if (q.acc.checkbox__c == false) {
                q.selected = true;
                q.acc.checkbox__c = true;
                update(q.acc);
                processSelected();
            }
        }
        return Null;
    }
    
    //UNSELECT all records in QueryList
    public PageReference unselectAll() {
        for(wrapAccount q: WrapCTAList) {
            if (q.acc.checkbox__c == true) {
                q.selected = false;
                q.acc.checkbox__c = false;
                update(q.acc);
                processSelected();
            }
        }
        return Null;
    }
    
    //Flow button, currently to Uncheck Boxes Flow
    public PageReference beginFlow() {
        for (wrapAccount s:tempWrapCTAList) {
            cta_database__c c = s.acc;
            selectedCTA.add(c);
        }
        return Page.CTA_Tab;
    }
    
    
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        
        public CTA_Database__c acc {get; set;}
        public Boolean selected {get; set;}
        
        public wrapAccount(CTA_Database__c a) {
            acc = a;
            selected = false;
        }
    }
    
    //----------------------------------//--------------------------------------//
    
    // the soql without the order and limit
    public String soql {get;set;}
    
    // the current sort direction. defaults to asc
    public String sortDir {
        get  { if (sortDir == null) {sortDir = 'asc';} return sortDir;}
        set;
    }
    
    // the current field to sort by. defaults to last name
    public String sortField {
        get  { if (sortField == null) {sortField = 'Last_Name__c'; } return sortField;  }
        set;
    }
    
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
        // run the query again
        runQuery();
        buildSelections();
    }
    
    // format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
        set;
    }      
    
    
    // runs the actual query
    public void runQuery() {
        
        wrapCTAList = new List<wrapAccount>();
        
        try {
            //builds QueryList
            for(CTA_Database__c obj : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200')) {
                wrapCTAList.add(new wrapAccount(obj));
                
                //sets Select property to TRUE if record is on SelectedList
                if (obj.Checkbox__c == true) {
                    for (wrapAccount c:wrapCTAList) {
                        string cName = c.acc.name;
                        if (obj.name == cName) {
                            c.selected = true;
                        }
                    }
                }
            }            
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        }
    }    
    
    // runs the search with parameters passed via Javascript
    public PageReference runSearch() {
        
        String memberNumber = Apexpages.currentPage().getParameters().get('memberNumber');
        String firstName = Apexpages.currentPage().getParameters().get('firstName');
        String lastName = Apexpages.currentPage().getParameters().get('lastName');
        String ChineseName = Apexpages.currentPage().getParameters().get('ChineseName');
        String PrimaryPhone = Apexpages.currentPage().getParameters().get('PrimaryPhone');
        String ResBuilding = Apexpages.currentPage().getParameters().get('ResBuilding');
        String ResUnit = Apexpages.currentPage().getParameters().get('ResUnit');        
        
        soql = 'select Name, First_Name__c, Last_Name__c, Chinese_Name__c, Primary_Phone__c, Residential_Building_Address__c, Residential_Unit__c, Checkbox__c, Family_Status__c FROM CTA_Database__c WHERE Id !=null';
        if (!memberNumber.equals(''))
            soql += ' and Name LIKE \''+String.escapeSingleQuotes(memberNumber)+'%\'';
        if (!firstname.equals(''))
            soql += ' and First_Name__c LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
        if (!lastName.equals(''))
            soql += ' and Last_Name__c LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
        if (!ChineseName.equals(''))
            soql += ' and Chinese_Name__c LIKE \''+String.escapeSingleQuotes(ChineseName)+'%\'';  
        if (!PrimaryPhone.equals(''))
            soql += ' and Primary_Phone__c LIKE \''+String.escapeSingleQuotes(PrimaryPhone)+'%\'';
        if (!ResBuilding.equals(''))
            soql += ' and Residential_Building_Address__r.display_address__c LIKE \''+String.escapeSingleQuotes(ResBuilding)+'%\'';
        if (!ResUnit.equals(''))
            soql += ' and Residential_Unit__r.Name LIKE \''+String.escapeSingleQuotes(ResUnit)+'%\'';
       
        // run the query again
        runQuery();
        return null;
    }
    
}