You need to sign in to do that
Don't have an account?
Pushkar Gupta
hello all , i have a question regarding the vf page
i want to create contact records using checkbox in vf page
Apex Controller:
public class AddmultipleContactsVF {
Contact Cont = new Contact();
public list<Contact> listContact{get;set;}
public Boolean IsSelected{set;get;}
public AddmultipleContactsVF()
{
listContact=new list<Contact>();
listContact.add(Cont);
}
Public void addContact()
{
Contact con = new Contact();
listContact.add(con);
}
public PageReference saveContact() {
for(Integer i=1; i<listContact.size(); i++)
{
if(Isselected==true){
upsert listContact;
}
}
return null;
}
public class wrapperContact{
public Boolean IsSelected{set;get;}
public wrapperContact(){
IsSelected=false;
}
}
}
VF Page:
<apex:page Controller="AddmultipleContactsVF">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listContact}" var="con">
<apex:column headerValue="Contact LastName">
<apex:inputCheckbox value="{!IsSelected}" id="InputId"/>
<apex:inputField value="{!con.LastName}"/>
<apex:CommandButton value="+" action="{!addContact}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:CommandButton value="Save Contact" action="{!saveContact}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller:
public class AddmultipleContactsVF {
Contact Cont = new Contact();
public list<Contact> listContact{get;set;}
public Boolean IsSelected{set;get;}
public AddmultipleContactsVF()
{
listContact=new list<Contact>();
listContact.add(Cont);
}
Public void addContact()
{
Contact con = new Contact();
listContact.add(con);
}
public PageReference saveContact() {
for(Integer i=1; i<listContact.size(); i++)
{
if(Isselected==true){
upsert listContact;
}
}
return null;
}
public class wrapperContact{
public Boolean IsSelected{set;get;}
public wrapperContact(){
IsSelected=false;
}
}
}
VF Page:
<apex:page Controller="AddmultipleContactsVF">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listContact}" var="con">
<apex:column headerValue="Contact LastName">
<apex:inputCheckbox value="{!IsSelected}" id="InputId"/>
<apex:inputField value="{!con.LastName}"/>
<apex:CommandButton value="+" action="{!addContact}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:CommandButton value="Save Contact" action="{!saveContact}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
public class AddmultipleContactsVF {
public class wrapperContact{
public Contact contact {get;set;}
public boolean isSelected{get;set;}
}
public list<wrapperContact> listContact{get;set;}
// public Boolean IsSelected{set;get;}
public AddmultipleContactsVF()
{
listContact=new list<wrapperContact>();
Contact Cont1 = new Contact();
wrapperContact Cont = new wrapperContact();
Cont.contact = Cont1;
Cont.isSelected = false;
listContact.add(Cont);
}
Public void addContact()
{
Contact nextContact = new Contact();
wrapperContact con = new wrapperContact();
con.contact=nextContact;
con.isSelected = false;
listContact.add(con);
}
public PageReference saveContact() {
//-- filter selected contact
List<Contact> selectedContactList = new List<Contact>();
for(Integer i=0; i<listContact.size(); i++)
{
if(listContact.get(i).Isselected){
selectedContactList.add(listContact.get(i).contact);
}
}
//-- upsert selected contact
if(selectedContactList!=null && !selectedContactList.isEmpty()){
upsert selectedContactList;
}
return null;
}
}
Visual Force
-------------------------
<apex:page Controller="AddmultipleContactsVF">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listContact}" var="con">
<apex:column headerValue="Contact LastName">
<apex:inputCheckbox value="{!con.IsSelected}" id="InputId"/>
<apex:inputField value="{!con.contact.LastName}"/>
<apex:CommandButton value="+" action="{!addContact}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:CommandButton value="Save Contact" action="{!saveContact}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Please mark it best of it helps you. Thanks.