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
Anjali Sharma 87Anjali Sharma 87 

One Account with multiple contact on vf page


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>
      <apex:column headerValue="Rating">
       <apex:inputField value="{!mywrapperobj[key].accobj.Rating}"/>
      </apex:column>
      <apex:column headerValue="Type">
       <apex:inputField value="{!mywrapperobj[key].accobj.Type}"/>
      </apex:column>
      </apex:pageBlockTable>
     <apex:pageBlockTable var="innerListVar" value="{!mywrapperobj[key].conlist}">
      <apex:column headerValue="Last Name">
       <apex:inputField value="{!innerListVar.LastName}"/>
      </apex:column>
     </apex:pageBlockTable>
    </apex:pageBlockSection>
    </apex:pageBlock>
 </apex:form>
</apex:page>


Controller***********

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>();
    addaccount();
   }

   public void addaccount(){
   counter ++;
   mywrapperobj.put(counter,new wrapperclass());
   row=counter;
   addcontactRow();
   }

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


Thanks in advance