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
Shubham Sinha 49Shubham Sinha 49 

Create a Visualforce page

I have a requirement where i need to create a VF that takes  last name as input and search for the contacts .Results should be displayed as selectable records with checkboxes.User should be able to select the results and should display the selected results upon hitting the See Results button below the record.

Please help me on that
Best Answer chosen by Shubham Sinha 49
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Try this code .Kindly modify according to your requirement.

Apex class
<apex:page controller="AccountSelectClassController" 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:inputText value="{!searchtext1}" label="Search" />
				<apex:pageBlockButtons >
                <apex:commandButton value="search" action="{!search}" rerender="table"/>
                     <apex:commandButton value="Show Selected contacts" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
	           
             
            <apex:pageblockSection title="All Contacts" collapsible="false" columns="2">
             
                <apex:pageBlockTable value="{!wrapContactList}" var="accWrap" id="table" title="All Contacts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.con.id}" />
                    <apex:column value="{!accWrap.con.name}" />
                    <apex:column value="{!accWrap.con.Phone}" />
                </apex:pageBlockTable>         
             
            </apex:pageblockSection>
             <apex:pageblockSection title="selected contacts" collapsible="false" columns="2">
             <apex:pageBlockTable value="{!selectedcontacts}" var="c" id="table2" title="Selected Contacts">
                    <apex:column value="{!c.id}" headerValue=" id"/>
                    <apex:column value="{!c.Name}" headerValue="name"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
                       
    </apex:form>
   
</apex:page>

Vf page
public class AccountSelectClassController{
//Our collection of the class/wrapper objects wrapAccount 
    public List<wrapContact> wrapContactList {get; set;}
    public List<contact> selectedcontacts{get;set;}
    public string searchtext1{get;set;}
     
    public void search(){
        if(wrapContactList == null) {
            wrapContactList = new List<wrapContact>();
            for(Contact a: [select Id, name, Phone from contact where lastname like :searchtext1 ]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapContactList.add(new wrapContact(a));
            }
        }
    }
 
    public void processSelected() {
    selectedcontacts = new List<contact>();
 
        for(wrapContact wrapContactObj : wrapContactList) {
            if(wrapContactObj.selected == true) {
                selectedcontacts.add(wrapContactObj.con);
            }
        }
    }
 
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapContact {
        public contact con {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapContact(contact a) {
            con = a;
            selected = false;
        }
    }
}

Please refer below link which might help you in this
http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Try this code .Kindly modify according to your requirement.

Apex class
<apex:page controller="AccountSelectClassController" 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:inputText value="{!searchtext1}" label="Search" />
				<apex:pageBlockButtons >
                <apex:commandButton value="search" action="{!search}" rerender="table"/>
                     <apex:commandButton value="Show Selected contacts" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
	           
             
            <apex:pageblockSection title="All Contacts" collapsible="false" columns="2">
             
                <apex:pageBlockTable value="{!wrapContactList}" var="accWrap" id="table" title="All Contacts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.con.id}" />
                    <apex:column value="{!accWrap.con.name}" />
                    <apex:column value="{!accWrap.con.Phone}" />
                </apex:pageBlockTable>         
             
            </apex:pageblockSection>
             <apex:pageblockSection title="selected contacts" collapsible="false" columns="2">
             <apex:pageBlockTable value="{!selectedcontacts}" var="c" id="table2" title="Selected Contacts">
                    <apex:column value="{!c.id}" headerValue=" id"/>
                    <apex:column value="{!c.Name}" headerValue="name"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
                       
    </apex:form>
   
</apex:page>

Vf page
public class AccountSelectClassController{
//Our collection of the class/wrapper objects wrapAccount 
    public List<wrapContact> wrapContactList {get; set;}
    public List<contact> selectedcontacts{get;set;}
    public string searchtext1{get;set;}
     
    public void search(){
        if(wrapContactList == null) {
            wrapContactList = new List<wrapContact>();
            for(Contact a: [select Id, name, Phone from contact where lastname like :searchtext1 ]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapContactList.add(new wrapContact(a));
            }
        }
    }
 
    public void processSelected() {
    selectedcontacts = new List<contact>();
 
        for(wrapContact wrapContactObj : wrapContactList) {
            if(wrapContactObj.selected == true) {
                selectedcontacts.add(wrapContactObj.con);
            }
        }
    }
 
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapContact {
        public contact con {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapContact(contact a) {
            con = a;
            selected = false;
        }
    }
}

Please refer below link which might help you in this
http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Shubham Sinha 49Shubham Sinha 49
Hi Chandrika,

I have gone through this link but my requirement is not to show all the accounts on the page ,User will search the contact by name then the selected record will be shown
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Try the code which i pasted above the link.It will display contacts based on the last name searched by user .Output will be like this 
User-added imageHope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
Shubham Sinha 49Shubham Sinha 49
I Tried the above code but the last name did not return any result. I have a Conatact on my org named 'Shubham Sinha'
Shubham Sinha 49Shubham Sinha 49
User-added image
united solutionsunited solutions
Please refer below link which might be helpful for you.
https://www.theunitedsolutions.in/what-is-wrapper-class-in-salesforce/

Hope You Liked It
Thanks.