+ Start a Discussion
Anjali Sharma 87Anjali Sharma 87 

One account with multiple contacts 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 
Best Answer chosen by Anjali Sharma 87
Suneel#8Suneel#8
Below is the VF page anc controller code for your requriement.Please check
<apex:page standardController="Account" extensions="AddContact">
    <apex:form id="myForm">
    <apex:panelGrid columns="2">
    Account Name: <apex:inputField value="{!account.name}" />
    </apex:panelGrid>
    <apex:repeat value="{!lContacts}" var="x">
    <apex:panelGrid columns="8">
    First Name:<apex:inputField value="{!x.c.firstName}" />
    Last Name:<apex:inputField value="{!x.c.lastName}" />    
    Lead Source:<apex:inputField value="{!x.c.leadSource}" /> 
    <apex:commandButton action="{!deleteContact}" style="Button" value="Delete Contact" reRender="myForm" immediate="true">
<apex:param value="{!x.counter}" name="selected"
assignTo="{!selectedContact}"/>
</apex:commandButton>      
    </apex:panelGrid>
    </apex:repeat>
    <apex:commandButton value="Add Contact" action="{!addAContact}" reRender="myForm" immediate="true"/>    
    <apex:commandButton value="Save" action="{!saveAccount}"  />    
    </apex:form> 
</apex:page>

Controller code
 
public class AddContact {
    ApexPages.StandardController sc;
    public Account acct{get;set;}
    public Integer marker=2;
    public Integer selectedContact{get;set;}
    public List<WrapperClass> lContacts{get;set;}
    public AddContact(ApexPages.StandardController controller) {
        this.acct = (Account)controller.getRecord();
        sc=controller;
        lContacts=new List<WrapperClass>();
        Contact c=new Contact();
        lContacts.add(new WrapperClass(c,1));
    }
    public PageReference deleteContact(){
        Integer x=-1;
        for(WrapperClass wc:lContacts){
            x++;
            if(wc.counter==selectedContact){
                System.debug('-->selected contact:'+selectedContact+'  position:'+x);
                break;
            }
        }
        lContacts.remove(x);
        return null;
    }
    public PageReference saveAccount(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Contact> contactList=new List<Contact>();
        for(WrapperClass wc:lContacts){
            Contact c=new Contact();
        c.lastName=wc.c.lastName;
        c.firstName=wc.c.firstName;
        c.accountid=idey;
        contactList.add(c);
        }
        insert contactList;
        
        return null;
    }
    public PageReference addAContact(){
        
        Contact c=new Contact();
        lContacts.add(new WrapperClass(c,marker));        
        marker=marker+1;
        return null;
    }
    public class WrapperClass{
        public Integer counter{get;set;}
        public Contact c{get;set;}
        public WrapperClass(Contact cntc,Integer i){
            this.c=cntc;
            this.counter=i;
        }
    }
}

 

All Answers

Suneel#8Suneel#8
Below is the VF page anc controller code for your requriement.Please check
<apex:page standardController="Account" extensions="AddContact">
    <apex:form id="myForm">
    <apex:panelGrid columns="2">
    Account Name: <apex:inputField value="{!account.name}" />
    </apex:panelGrid>
    <apex:repeat value="{!lContacts}" var="x">
    <apex:panelGrid columns="8">
    First Name:<apex:inputField value="{!x.c.firstName}" />
    Last Name:<apex:inputField value="{!x.c.lastName}" />    
    Lead Source:<apex:inputField value="{!x.c.leadSource}" /> 
    <apex:commandButton action="{!deleteContact}" style="Button" value="Delete Contact" reRender="myForm" immediate="true">
<apex:param value="{!x.counter}" name="selected"
assignTo="{!selectedContact}"/>
</apex:commandButton>      
    </apex:panelGrid>
    </apex:repeat>
    <apex:commandButton value="Add Contact" action="{!addAContact}" reRender="myForm" immediate="true"/>    
    <apex:commandButton value="Save" action="{!saveAccount}"  />    
    </apex:form> 
</apex:page>

Controller code
 
public class AddContact {
    ApexPages.StandardController sc;
    public Account acct{get;set;}
    public Integer marker=2;
    public Integer selectedContact{get;set;}
    public List<WrapperClass> lContacts{get;set;}
    public AddContact(ApexPages.StandardController controller) {
        this.acct = (Account)controller.getRecord();
        sc=controller;
        lContacts=new List<WrapperClass>();
        Contact c=new Contact();
        lContacts.add(new WrapperClass(c,1));
    }
    public PageReference deleteContact(){
        Integer x=-1;
        for(WrapperClass wc:lContacts){
            x++;
            if(wc.counter==selectedContact){
                System.debug('-->selected contact:'+selectedContact+'  position:'+x);
                break;
            }
        }
        lContacts.remove(x);
        return null;
    }
    public PageReference saveAccount(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Contact> contactList=new List<Contact>();
        for(WrapperClass wc:lContacts){
            Contact c=new Contact();
        c.lastName=wc.c.lastName;
        c.firstName=wc.c.firstName;
        c.accountid=idey;
        contactList.add(c);
        }
        insert contactList;
        
        return null;
    }
    public PageReference addAContact(){
        
        Contact c=new Contact();
        lContacts.add(new WrapperClass(c,marker));        
        marker=marker+1;
        return null;
    }
    public class WrapperClass{
        public Integer counter{get;set;}
        public Contact c{get;set;}
        public WrapperClass(Contact cntc,Integer i){
            this.c=cntc;
            this.counter=i;
        }
    }
}

 
This was selected as the best answer
Anjali Sharma 87Anjali Sharma 87
Thanks @Suneel Sir  it will we great help for me 
thanks again :)
Anjali Sharma 87Anjali Sharma 87
@Suneel Sir can you please tell me code if we want to enter a multiple account with multiple contact because this code only work for single account