+ Start a Discussion
Shriwastab SinghShriwastab Singh 

Dynamic Visualforce page to add multiple contact with checkbox .

i want to create a button add contact when i clicked one more empty contact show in my page.
like this screen shot.
User-added image
Best Answer chosen by Shriwastab Singh
Akshay_DhimanAkshay_Dhiman
Hi Shriwastab,

try this code its working fine
 
Visualforce page
<apex:page controller="wrapperClassController222">
    <apex:form >
        
        <apex:pageBlock id="BlockId" >
             <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save Contact" action="{!saveContact}" reRender="BlockId"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!contactList}" var="contact" id="table">
                <apex:column headerValue="Select">
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!contact.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column headerValue="Contact LastName">
                    <apex:inputField value="{!contact.con.LastName}"/>
                </apex:column>
                <apex:column headerValue="Add Contact">
                    <apex:CommandButton value="+" action="{!addContact}" Rerender="table" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

APex controller :
 
 public class wrapperClassController222 {
    public List<wrapContact> contactList {get; set;}
    
    public wrapperClassController222() {
        contactList = new List<wrapContact>();
        Contact Cont = new Contact();
        contactList.add(new wrapContact(Cont));
        
    }
    Public PageReference addContact()
    {
        Contact con = new Contact();
        contactList.add(new wrapContact(con));
        return null; 
    }
    
    
    public PageReference saveContact() {
        
        List<Contact> selectedContacts = new List<Contact>();
        for(wrapContact cCon: contactList) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
        if(selectedContacts.size() > 0){
            insert selectedContacts;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Saves succesfully'));
            contactList=null;
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Select contact'));
        }
        return null;
    }
    
    public class wrapContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        public wrapContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}



if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay

All Answers

PawanKumarPawanKumar
You can get the solution from here. Just now i posted the correct solution here. You can copy paste as it and then you can adjust your view as per your need. Thanks.
https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005iglQAA

Please mark it best if it helps you. Thanks.
Shriwastab SinghShriwastab Singh
@Pawan  thanks for quikly responce but my page design is diffrent .
Akshay_DhimanAkshay_Dhiman
Hi Shriwastab,

try this code its working fine
 
Visualforce page
<apex:page controller="wrapperClassController222">
    <apex:form >
        
        <apex:pageBlock id="BlockId" >
             <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save Contact" action="{!saveContact}" reRender="BlockId"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!contactList}" var="contact" id="table">
                <apex:column headerValue="Select">
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!contact.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column headerValue="Contact LastName">
                    <apex:inputField value="{!contact.con.LastName}"/>
                </apex:column>
                <apex:column headerValue="Add Contact">
                    <apex:CommandButton value="+" action="{!addContact}" Rerender="table" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

APex controller :
 
 public class wrapperClassController222 {
    public List<wrapContact> contactList {get; set;}
    
    public wrapperClassController222() {
        contactList = new List<wrapContact>();
        Contact Cont = new Contact();
        contactList.add(new wrapContact(Cont));
        
    }
    Public PageReference addContact()
    {
        Contact con = new Contact();
        contactList.add(new wrapContact(con));
        return null; 
    }
    
    
    public PageReference saveContact() {
        
        List<Contact> selectedContacts = new List<Contact>();
        for(wrapContact cCon: contactList) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
        if(selectedContacts.size() > 0){
            insert selectedContacts;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Saves succesfully'));
            contactList=null;
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Select contact'));
        }
        return null;
    }
    
    public class wrapContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        public wrapContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}



if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay
This was selected as the best answer