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 

Wrapper Class and Visualforce

Having some issues pushing records from my LIST of sObjects to a Map of boolean values and sobjects.  Any advice?

public with sharing class WrapperClass{
    
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapCTAList {get;set;}
    public Map<boolean,CTA_Database__c> ctaMap {get {
        if(ctaMap == null) {
            ctaMap = new Map<boolean, CTA_Database__c>();
        }
        return ctaMap;
    }
                                                set {
                                                    for (CTA_Database__c member:ctaMember) {
                                                        boolean selected = false;
                                                        ctaMap.put(selected, member);
                                                    }
                                                }
                                               }
    public List<CTA_Database__c> selectedCTA{get;set;}
    public List<CTA_Database__c> ctaMember{get;set;}
    
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        //empty Constructor           
    } 
    
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        Map<boolean,CTA_Database__c> ctaMap = new Map<boolean,CTA_Database__c>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        List<CTA_Database__c> ctaMember = new list<CTA_Database__c>();
        
        if(wrapCTAList == null) {
            for(CTA_Database__c a: [SELECT ID, Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapCTAList.add(new wrapAccount(a));
                for (wrapAccount wrapcta : wrapCTAList) {
                    ctaMap.put(wrapcta.selected = false, wrapcta.acc);
                }
            }
        }
        soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null';
        runQuery();
        buildMap();
    }
    
    public void buildMap() {
        try {
            for (CTA_Database__c member: ctaMember) {
                boolean selected = false;
                ctaMap.put(selected, member);
            }            
        } catch (exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Could not build ctaMap!'));
        }   

    }
    
    public void processSelected() {
        List<wrapAccount> wrapCTAList = new List<wrapAccount>();
        for(wrapAccount wrapcta : wrapCTAList) {
            if(wrapcta.selected == true) {
                selectedCTA.add(wrapcta.acc);
            }
        }
    }
    
    // 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 Map<boolean, CTA_Database__c> ctaMap {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();
    }
    // format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
        set;
    }      
    // init the controller and display some sample data when the page loads
    //public WrapperClass() {
    //soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null';
    //runQuery();
    //}
    
    // runs the actual query
    public void runQuery() {
        
        try {
            ctaMember = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200');
            buildMap();
        } 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 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();
        system.debug('hi');
        return null;
    }
}
Nayana KNayana K
Hi,

Please let me know exact requirement. Any specific reason for using map with boolean key?
Suppose if we have 3 records A, B, C

This code block
for (CTA_Database__c member:ctaMember) {
                                                        boolean selected = false;
                                                        ctaMap.put(selected, member);
                                                    }

will result in only one key-value pair in map. i.e ctaMap will have (false, C);
When we say boolean, only 2 possibilities present. Key can be either TRUE or FALSE.
When A comes inside loop, MAP => (false, A)
When B comes inside loop, MAP => (false, B) => Key already exists so override will happen
When C comes inside loop, MAP => (false, C) => Key already exists so override will happen


 
Harrison LauHarrison Lau
Hi Nayana, 

Thanks so much for your help!  Here's a quick description of the use case:

We are trying to implement some sort of hybrid between a dynamic search page (example:http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/) and a wrapper class.  The idea is that we can use the dynamic search functionality to filter down a list of records, then use a wrapper class to make selections (from the filtered list) pushing the selected records into a flow with the use of a button (or something like that).

My thinking on the Map with boolean key was that this collection would hold the boolean value (of my wrapper class) and the SObject Id for the record I want to select.  After your explanation I see now that this wasn't the best way to go about this project.  Do you have any suggestions you would be able to recommend? 

 
Harrison LauHarrison Lau
Quick Edit (for clarification):

Dynamic Search will filter the LIST called ctaMember.

wrapCTAList should be a mirror of the filtered ctaMember List -- this is a list of wrapper Objs.  This list should contain the boolean (selected = true/false) and the ID of the custom object CTA_Database__c for each item in the ctaMember list.  

selectedCTA should hold all values of wrapCTAList where the boolean 'selected' is true.  

Unless you see any other glaring errors, I think my main area of difficulty is pushing values from ctaMember to wrapCTAList -- I am unable to display the values on my VF page.  
Nayana KNayana K
public with sharing class WrapperClass {
	
	//Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapCTAList {get;set;}
	public List<CTA_Database__c> lstSelected {get;set;}
	
  // the soql without the order and limit
  private String soql {get;set;}

  // 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 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 = 'lastName'; } return sortField;  }
    set;
  }

  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
  }

  // init the controller and display some sample data when the page loads
  public WrapperClass() {
	// When we are searching from Database Id will never be null. So Id != NULL condition is meaningless here.
    soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c';
    runQuery();
  }
	public WrapperClass(ApexPages.StandardController WrapperClass) {
        //empty Constructor           
    } 
	
  // 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();
  }

  // runs the actual query
  public void runQuery() {
	wrapCTAList = new List<wrapAccount>();
    try {
      for(CTA_Database__c obj : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'))
	  {
		wrapCTAList.add(new wrapAccount(obj));
	  }
	   
    } 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 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();
        system.debug('hi');
        return null;
    }
	
	// Suppose we have a button say 'Process selected' and we are calling this method once we select/deselect records
	public void processSelected()
	{
		lstSelected = new List<CTA_Database__c>();
		for(wrapAccount objWrap: wrapCTAList)
		{
			// if record is selected in page (apex:inputCheckbox we can bind in page)
			if(objWrap.selected)
			{
				lstSelected.add(objWrap.acc);
			}
		}
		
		system.debug('==lstSelected=='+lstSelected);
		// use lstSelected now as per your requirement.
	}
}

I think you just need a checkbox to select record displayed on table. If that's the scenario then please use above code.
I think you are already done with VF page. Just modify  pageblocktable little bit and then extra button which I have shown.
 
<apex:page controller="WrapperClass" sidebar="false">

  <apex:form >
  <apex:pageMessages id="errors" />

  <apex:pageBlock title="Find Me A Customer!" mode="edit">

  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top">

      <apex:pageBlock title="Parameters" mode="edit" id="criteria">

      <script type="text/javascript">
      function doSearch() {
	  <!--Change below as per your requirement -->
        searchServer(
          document.getElementById("firstName").value,
          document.getElementById("lastName").value,
          document.getElementById("accountName").value,
          document.getElementById("technology").options[document.getElementById("technology").selectedIndex].value
          );
      }
      </script> 

      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
		<!--Change below as per your requirement -->
          <apex:param name="firstName" value="" />
          <apex:param name="lastName" value="" />
          <apex:param name="accountName" value="" />
          <apex:param name="technology" value="" />
      </apex:actionFunction>

      <table cellpadding="2" cellspacing="2">
	  <!--Change below  TR as per your requirement -->
      <tr>
        <td style="font-weight:bold;">First Name<br/>
        <input type="text" id="firstName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Last Name<br/>
        <input type="text" id="lastName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Account<br/>
        <input type="text" id="accountName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Interested Technologies<br/>
          <select id="technology" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!technologies}" var="tech">
              <option value="{!tech}">{!tech}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>

      </apex:pageBlock>

    </td>
    <td valign="top">

    <apex:pageBlock mode="edit" id="results">

        <apex:pageBlockTable value="{!wrapCTAList}" var="objWrap">

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="First Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="firstName" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:inputCheckbox value="{!objWrap.selected}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Last Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="lastName" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!objWrap.acc.Name}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Account" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!objWrap.acc.First_Name__c}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Technologies" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="interested_technologies__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!objWrap.acc.Last_Name__c}"/>
            </apex:column>
			<!--Rest of columns you can add here -->
        </apex:pageBlockTable>
		
    </apex:pageBlock>

    </td>
  </tr>
  </table>

  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />           
  </apex:pageBlock>    

  </apex:pageBlock>
	<!-- HERE I KEPT BUTTON WHICH I WAS TALING ABOUT. On click of this check debug of processSelected method -->
	<apex:commandButton value=Process Selected" action="{!processSelected}" rerender="results"/>
  </apex:form>

</apex:page>

 
Harrison LauHarrison Lau
Thanks so much for all your help Nayana!
The page and controller look just as they should!  Only one problem now:  my query is not working!
I took some time to poke around older versions of this code where I had the "search" functionality working and it appears to be the same as what we have here.  Not entirely sure what's going on but the onkeyup() functions don't seem to be firing.  Any suggestions?
Nayana KNayana K
Appologies for the delayed response.
Pelase paste whole code here and let me know the line number which you feel there is a problem. I will do the needful.
Harrison LauHarrison Lau
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<CTA_Database__c> ctaMember{get;set;}
    
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        //empty Constructor           
    } 
    
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        List<CTA_Database__c> ctaMember = 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 FROM CTA_Database__c WHERE Id != null';
        runQuery();
    }
    
    public void buildList() {
        try {
            for (CTA_Database__c member: ctaMember) {
                //List<wrapAccount> wrapCTAList = new List<CTA_Database__c>(ctaMember);
                wrapCTAList.add(new wrapAccount(member));
            }            
        } catch (exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Could not build ctaMap!'));
        }   
        
    }
    
    public void processSelected() {
        if(ctaMember == null) {
            ctaMember = new List<CTA_Database__c>();
            for(wrapAccount objWrap: wrapCTAList) {
                // if record is selected in page (apex:inputCheckbox we can bind in page)
                if(objWrap.selected) {
                    ctaMember.add(objWrap.acc);
                }
            }
            system.debug('==ctaMember=='+ctaMember);
        } else {
            if(ctaMember !=null) {
                for(wrapAccount objWrap:wrapCTAList) {
                    if (objWrap.selected) {
                        ctaMember.add(objWrap.acc);                         
                    }
                }
            }           
        }
    }  
          
    
    
    // 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 Map<boolean, CTA_Database__c> ctaMap {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();
    }
    // 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 {
            for(CTA_Database__c obj : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200')) {
                wrapCTAList.add(new wrapAccount(obj));
            }
        } 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 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();
        system.debug('hi');
        return null;
    }
}


Sorry I'm not sure how to display code nicely like your's (with the line numbers).  I've BOLDED the section I'm working on now.  

I was able to get the "dynamic search" part working again, but I am having a problem with my processSelected function.  The function works insofar as I can select records from one list and then use the button to push them to the other list.  HOWEVER, I would like to make it so that each record that I select is only added when that record does not already exist on the other list.  Essentially, I need the processSelected function to do some de-duping -- it should check the value of the Name field (aka memberNumber) on the wrapCTAList record to make sure that it doesn't already exist on the ctaMember list before it adds values.  If selected, for each record that does NOT exist on ctaMember, a new one should be created; records that already exist on ctaMember should be skipped.  Is this possible?
Nayana KNayana K
List<wrapAccount> wrapCTAListSelected {get;set;}

 public void processSelected() {
		wrapCTAListSelected = new List<wrapAccount>();
		List<wrapAccount> tempWrapCTAList = new List<wrapAccount>();

            for(wrapAccount objWrap: wrapCTAList) {
                if(objWrap.selected) {
                    wrapCTAListSelected.add(objWrap);
                }
				else
				{
					tempWrapCTAList.add(objWrap);
				}
            }
			// now this will contain only unselected ones	
			wrapCTAList.addAll(tempWrapCTAList);
                   
        
    }

 
Harrison LauHarrison Lau
Here's another question...  (first the code again)

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<CTA_Database__c> ctaMember{get;set;}
    public LIST<wrapAccount> wrapCTAListSelected {get;set;}
    public List<wrapAccount> tempWrapCTAList{get;set;}

    
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        //empty Constructor           
    } 
    
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        List<CTA_Database__c> ctaMember = new list<CTA_Database__c>();
        List<WrapAccount> WrapCTAListSelected = new List<WrapAccount>();
        
        soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c';
        runQuery();
    }


// Pushes all 'selected' values from wrapCTAList to wrapCTAListSelected
 public void processSelected() {
        wrapCTAListSelected = new List<wrapAccount>();
        List<wrapAccount> tempWrapCTAList = new List<wrapAccount>();

            for(wrapAccount objWrap: wrapCTAList) {
                if(objWrap.selected) {
                    wrapCTAListSelected.add(objWrap);
                }
                else
                {
                    tempWrapCTAList.add(objWrap);
                }
            }
            // contains unselected records (filtered from runQuery)
            wrapCTAList.addAll(tempWrapCTAList);
    }

               
    
    // This is our wrapper/container class. In this example a wrapper class contains custom object cta_database__c 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();
    }
    // format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
        set;
    }      
    
    
    // runs database queries based on values passed in the runSearch function.
    public void runQuery() {
        wrapCTAList = new List<wrapAccount>();
        try {
            for(CTA_Database__c obj : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200')) {
                wrapCTAList.add(new wrapAccount(obj));
            }
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        }
    }

    
    
    
    // runs the search with parameters passed via Javascript -- passes new soql value into runQuery if the field is changed on VFpage
    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 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;
    }
}



I think the issue I've been having stems from the fact that my list of records (wrapCTAList -- from which I make selections) gets refreshed each time I enter new search parameters.  For this case, I think the best solution would be to have some way to persist my selection option  to the list that is rerendered when the runQuery function completes.  Is there any way to do this?  

I was thinking it would need to be somewhere in the runQuery function itself, or in the processSelected function but I could be wrong.  What do you suggest?