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
niharnihar 

How to add select check box after displaying Lead and Contact records in visualforce page

Hi All,
How to add select check box after displaying Lead and Contact records in visualforce page.
I have written the apex class and visual force page for displaying records both leads and contacts here is my code..........
apex class:
public class objectcontroller
{
    public List<selectOption> objects{get; set;}
    public string selected{get; set;}
    public List<contact> obj1{get; set;}
    public list<lead> obj2{get; set;}
    public boolean obj1Status{get; set;}
    public boolean obj2Status{get; set;}
    public objectcontroller()
    {
        obj1Status=false;
        obj2Status=false;
        objects= new List<selectOption>();
        objects.add(new selectOption('contact','contact'));
        objects.add(new selectOption('lead','lead'));
    }
    public pageReference getRecordofObject()
    {
         if(selected=='contact')
        {
            obj1=[select id,lastname,Email from contact limit 100];
            obj1Status=true;
        }   
        else
        {
            obj2=[select name,company,Status from lead limit 100];
            obj2Status=true;
        }
        return null;
    }
    
}

Visualforce page:
<apex:page controller="objectcontroller" showHeader="false">
    
    <apex:form >
        
        <apex:pageBlock title="Select a object">
            <apex:selectList value="{!selected}" size="1">
                <apex:selectOptions value="{!objects}"/>
            </apex:selectList>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!getRecordofObject}" value="Show Records" rerender="cb"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:outputPanel >
            <apex:pageBlock id="cb">
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!obj1}" var="ob1" rendered="{!obj1Status}">
                        <apex:column value="{!ob1.id}"/>
                        <apex:column value="{!ob1.lastname}"/>
                        <apex:column value="{!ob1.Email}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>  
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!obj2}" var="ob2" rendered="{!obj2Status}">
                        <apex:column value="{!ob2.id}"/>
                        <apex:column value="{!ob2.company}"/>
                        <apex:column value="{!ob2.status}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>
Best Answer chosen by nihar
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi nihar,
Have to use wrapper class to add checkboxes while displaying records in vf page.I have made changes in your code.Modify according to your requirements.
<apex:page controller="objectcontroller" showHeader="false">
    <apex:form >
        <apex:pageBlock title="Select a object">
            <apex:selectList value="{!selected}" size="1">
                <apex:selectOptions value="{!objects}"/>
            </apex:selectList>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!getRecordofObject}" value="Show Records" rerender="cb"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:outputPanel >
            <apex:pageBlock id="cb">
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!wrapcon}" var="ob1" rendered="{!obj1Status}">
                        <apex:column >
                            <apex:inputCheckbox value="{!ob1.selected}" id="inputId"/>
                        </apex:column>
                        <apex:column value="{!ob1.con.id}"/>
                        <apex:column value="{!ob1.con.lastname}"/>
                        <apex:column value="{!ob1.con.Email}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>  
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!wrapLd}" var="ob2" rendered="{!obj2Status}">
                        <apex:column >
                            <apex:inputCheckbox value="{!ob2.selected}" id="inputId"/>
                        </apex:column>
                        <apex:column value="{!ob2.Ld.id}"/>
                        <apex:column value="{!ob2.Ld.company}"/>
                        <apex:column value="{!ob2.Ld.status}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>
controller
public class objectcontroller
{
    public List<selectOption> objects{get; set;}
    public string selected{get; set;}
    public List<contact> obj1{get; set;}
    public list<lead> obj2{get; set;}
    public boolean obj1Status{get; set;}
    public boolean obj2Status{get; set;}
    public list<wrapcontact> wrapcon{get;set;}
    public list<wrapLead> wrapLd{get;set;}
    public objectcontroller()
    {
        obj1Status=false;
        obj2Status=false;
        objects= new List<selectOption>();
        objects.add(new selectOption('contact','contact'));
        objects.add(new selectOption('lead','lead'));
        wrapcon = new List<wrapcontact>();
        wrapLd = new list<wrapLead>();
    }
    public pageReference getRecordofObject()
    {
         if(selected=='contact')
        {
            for(contact c :[select id,lastname,Email from contact limit 100]){
            wrapcon.add(new wrapcontact(c));
            }
            obj1Status=true;
        }   
        else
        {
            for(lead l:[select name,company,Status from lead limit 100]){
                wrapLd.add(new wrapLead(l));
            }
            obj2Status=true;
        }
        return null;
    }
    public class wrapcontact {
        public contact con {get; set;}
        public Boolean selected {get; set;}
 
        public wrapcontact(contact a) {
            con = a;
            selected = false;
        }
    }
    public class wraplead{
        public lead Ld {get; set;}
        public Boolean selected {get; set;}
 
        public wraplead(lead a) {
            Ld = a;
            selected = false;
        }
    }
    
}

Please refer below links which might help you further.
https://developer.salesforce.com/forums/?id=906F0000000DBmuIAG
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) 
Hi nihar,
Have to use wrapper class to add checkboxes while displaying records in vf page.I have made changes in your code.Modify according to your requirements.
<apex:page controller="objectcontroller" showHeader="false">
    <apex:form >
        <apex:pageBlock title="Select a object">
            <apex:selectList value="{!selected}" size="1">
                <apex:selectOptions value="{!objects}"/>
            </apex:selectList>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!getRecordofObject}" value="Show Records" rerender="cb"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:outputPanel >
            <apex:pageBlock id="cb">
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!wrapcon}" var="ob1" rendered="{!obj1Status}">
                        <apex:column >
                            <apex:inputCheckbox value="{!ob1.selected}" id="inputId"/>
                        </apex:column>
                        <apex:column value="{!ob1.con.id}"/>
                        <apex:column value="{!ob1.con.lastname}"/>
                        <apex:column value="{!ob1.con.Email}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>  
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!wrapLd}" var="ob2" rendered="{!obj2Status}">
                        <apex:column >
                            <apex:inputCheckbox value="{!ob2.selected}" id="inputId"/>
                        </apex:column>
                        <apex:column value="{!ob2.Ld.id}"/>
                        <apex:column value="{!ob2.Ld.company}"/>
                        <apex:column value="{!ob2.Ld.status}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>
controller
public class objectcontroller
{
    public List<selectOption> objects{get; set;}
    public string selected{get; set;}
    public List<contact> obj1{get; set;}
    public list<lead> obj2{get; set;}
    public boolean obj1Status{get; set;}
    public boolean obj2Status{get; set;}
    public list<wrapcontact> wrapcon{get;set;}
    public list<wrapLead> wrapLd{get;set;}
    public objectcontroller()
    {
        obj1Status=false;
        obj2Status=false;
        objects= new List<selectOption>();
        objects.add(new selectOption('contact','contact'));
        objects.add(new selectOption('lead','lead'));
        wrapcon = new List<wrapcontact>();
        wrapLd = new list<wrapLead>();
    }
    public pageReference getRecordofObject()
    {
         if(selected=='contact')
        {
            for(contact c :[select id,lastname,Email from contact limit 100]){
            wrapcon.add(new wrapcontact(c));
            }
            obj1Status=true;
        }   
        else
        {
            for(lead l:[select name,company,Status from lead limit 100]){
                wrapLd.add(new wrapLead(l));
            }
            obj2Status=true;
        }
        return null;
    }
    public class wrapcontact {
        public contact con {get; set;}
        public Boolean selected {get; set;}
 
        public wrapcontact(contact a) {
            con = a;
            selected = false;
        }
    }
    public class wraplead{
        public lead Ld {get; set;}
        public Boolean selected {get; set;}
 
        public wraplead(lead a) {
            Ld = a;
            selected = false;
        }
    }
    
}

Please refer below links which might help you further.
https://developer.salesforce.com/forums/?id=906F0000000DBmuIAG
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
niharnihar
Hi Devi Chandrika,
 Thanks for the code and i have small modifications in the code can you help me with that......
Actually when i choose one object lead or contact only one object records should be displayed but two objects records are displaying can you help me with the requirment.......................
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Nihar,
You just have to make other object pageBlockTable rendered attribute to false.
Try this one.
public pageReference getRecordofObject()
    {
         if(selected=='contact')
        {
            for(contact c :[select id,lastname,Email from contact limit 100]){
            wrapcon.add(new wrapcontact(c));
            }
            obj1Status=true;
            obj2status=false;
        }   
        else
        {
            for(lead l:[select name,company,Status from lead limit 100]){
                wrapLd.add(new wrapLead(l));
            }
            obj1status=false;
            obj2Status=true;
        }
        return null;
    }

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

Thanks and Regards
niharnihar
Hi Devi Chandrika ,
Thanks For your wonder full time actually in last conversation i forgot to mention one small requirment if you have time you can help me with that...
Actually i want to add Previous and Next buttons after dipalying the records..
Example if have 50 records first it will displayed only 10 records for next records we should have Next Or previous buttons...........
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Please refer below links which might help you in implementing pagination in your vf page.
http://amitsalesforce.blogspot.com/search/label/Pagination
https://redpointcrm.com/add-pagination-to-your-visualforce-pages-using-the-soql-offset-clause

Hope this helps you
Thanks and Regards
John KlokJohn Klok
Hey,

Please refer the crmjetty site and salesforce blog which might you help the customization solustion for salesforce.

https://www.crmjetty.com/blog/salesforce/
https://www.crmjetty.com/salesforce-wordpress-customer-portal.htm