+ Start a Discussion
Anjali Sharma 87Anjali Sharma 87 

Adding a row for account and adding multiple row for the multiple contact with add and del buttons

Can anyone please tell me if i want to add a row in which three fields are their i.e Account Name , Industry and Rating and two buttons and add row and del row.When a user insert the value in these fields and click on the add row button then these field and again show the text box to enter the value and simultaneously then the contact fields also show few text box on the visualforce page and two button for contact records when user click on add contact then the one more contact is added to same account and when delete it will delete the one contact....

I have create the code but that was not working properly so please help me for solving these tasks..

User-added image
Instead of dynamically populating rows,I guess it is better to have only one row with input text boxes and create a button "Insert record" to save the record.You can have a table at the bottom of the page which will contain the records user added till then.Method that "Insert record" button is calling will take the inputs from the text boxes and save the record into database and refresh the table at the botton. That way you just have to add a table in the bottom of the page to complete your usecase.Just a thought :)
Balaji Chowdary GarapatiBalaji Chowdary Garapati

@Siddhartha Tomar 3:

You can use a wrapper class to acheive this, Consider the below example:


Public Class DemoWrapperClassFunctionality{
  Public DemoWrapperClassFunctionality(){
VFDetailsWrprList=new List<VFDetails_Wrapper>();

// In wrapper class you can have your desired variables, for now i just used account and contact details
Public Class  VFDetails_Wrapper{ 
 Pubilc Account account_info{get;set;}
Public Contact contact_info{get;set;}
Public VFDetails_Wrapper(Account account,Contact contact){

Public List<VFDetails_Wrapper> VFDetailsWrprList{get;set;}

Public pageReference addRow(){
VFDetailsWrprList.add(new VFDetails_Wrapper(new Account(),new Contact()));


VF Page:

<apex:pageBlocktable value="{!VFDetailsWrprList}" var="VFDetail">

  {!VFDetail.account_info.BillingState} // you can use your desired tag type to have this value applied.

<apex:commandbutton action="{!addRow}" value="Add Row" />

For deleting a row, it depends on your requirement, whether to be able to delte a specific row of last row that was added which determines whether to use any script on how to handle that or just a constructor method.

Hope this helps:


Anjali Sharma 87Anjali Sharma 87
@ Balaji

Thanks for replying me but actually my reqiurement is that we have one add account button and del button at time for page load 
When the user click on the add new account then few fields ex: Account name ,Industry and Rating are show with inputFields so the user can
enter the user value and then two more button are display on page to add the contact for that account and delete the contact for that account.
For example one account with multiple contact on row.I have try but actully i have used the two pageblocktable with the Map in which i receive the value in one pageblocktable but when i am using the contact value in pageblocktable it is given the error i.e Map key null not found in map.
I am sharing the code ...

Visualforce page*************

<apex:page controller="Addcontactwrappernewcontroller">
 <apex:form >
   <apex:commandButton value="Add Account" action="{!addaccount}" rerender="panel"/>
    <apex:pageBlock >
    <apex:pageBlockSection columns="2">
     <apex:pageBlockTable var="key" value="{!mywrapperobj}">
      <apex:column headerValue="Account Name">
       <apex:inputField value="{!mywrapperobj[key].accobj.Name}"/>
      <apex:column headerValue="Rating">
       <apex:inputField value="{!mywrapperobj[key].accobj.Rating}"/>
      <apex:column headerValue="Type">
       <apex:inputField value="{!mywrapperobj[key].accobj.Type}"/>
     <apex:pageBlockTable var="innerListVar" value="{!mywrapperobj[key].conlist}">
      <apex:column headerValue="Last Name">
       <apex:inputField value="{!innerListVar.LastName}"/>


public with sharing class Addcontactwrappernewcontroller {

    public String getKey() {
        return null;

    public Integer counter {get;set;}
    public Integer row {get;set;}
    public Map<Integer,wrapperclass> mywrapperobj {get;set;}

    public class wrapperclass{
    public List<Contact> conlist{get;set;}
    public Account accobj{get;set;}
    public wrapperclass(){
    this.conlist = new List<Contact>();
    this.accobj = new Account();

    public Addcontactwrappernewcontroller(){
    counter = 0;
    mywrapperobj = new Map<Integer,wrapperclass>();

   public void addaccount(){
   counter ++;
   mywrapperobj.put(counter,new wrapperclass());

   public void addcontactRow(){
   mywrapperobj.get(row).conlist.add(new Contact());

Thanks in advance @Balaji