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
sf.dev.ax1103sf.dev.ax1103 

create multiple contact records from vf page

Hi,

 

     I need to create multiple contact records from vf page.Any code plzz?

 

Thanks

Chamil MadusankaChamil Madusanka

Please explain your requirement..

sf.dev.ax1103sf.dev.ax1103

My requirement is to create multiple contacts from vf page and also have add row capabality  by which they could add one more row of contact.This makes the user to create multiple contacts in one shot.

Chamil MadusankaChamil Madusanka

Hi,

 

Try this example,

 

<apex:page controller="ManageListController2">
 <apex:form >
 <apex:pageBlock title="Specific Dates for Training Plan">
 <apex:pageBlockSection title="Specific Dates for Training Plan">
    <!-- <apex:pageblockSectionItem >
         <apex:outputLabel value="Training Plan" for="tp"></apex:outputLabel>
         <apex:inputText id="tp" value="{!wrappers.acc.Training_Plan__c}"/>
     </apex:pageblockSectionItem> -->
     <apex:pageblockSectionItem >
         <apex:outputLabel value="Number of Rows" for="noofdays"></apex:outputLabel>
         <apex:inputText id="noofdays" value="{!noOfDays}"/>
     </apex:pageblockSectionItem>
     <apex:pageBlockSectionItem >
         <apex:commandButton value="Add Rows" action="{!addRows}" rerender="wtable">
             <apex:param name="addCount" value="{!noOfDays}" assignTo="{!addCount}"/>
         </apex:commandButton>
     </apex:pageBlockSectionItem>
 </apex:pageBlockSection>
   
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Last Name">
            <apex:inputField value="{!wrapper.acc.LastName}"/>
         </apex:column>
         <apex:column headerValue="Email">
            <apex:inputField value="{!wrapper.acc.Email}"/>
         </apex:column>
         <apex:column headerValue="Mobile">
            <apex:inputField value="{!wrapper.acc.MobilePhone}"/>
         </apex:column>
         
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
            </apex:commandButton>
         </apex:column>
      </apex:pageBlockTable>
    <!--  <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton> -->
      
      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlock>
 </apex:form>
</apex:page>

 

public class ManageListController2
{

 public String noOfDays { get; set; }
 public List<AccountWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=1;
  
 public ManageListController2()
 {
  wrappers=new List<AccountWrapper>();
  for (Integer idx=0; idx<0; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
  
 public void addRows()
 {

 addCount = Integer.valueOf(noOfDays);
  System.Debug('addCount ::: '+addCount);
  for (Integer idx=1; idx<=addCount; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public PageReference save()
 {
  List<Contact> accs=new List<Contact>();
  for (AccountWrapper wrap : wrappers)
  {
   accs.add(wrap.acc);
  }
   
  insert accs;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Contact').getDescribe().getKeyPrefix() + '/o');
 }
  
 public class AccountWrapper
 {
  public Contact acc {get; private set;}
  public Integer ident {get; private set;}
   
  public AccountWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new Contact();
  }
 }
}

 I think this will help you to resolve your problem.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

Ritika JRitika J

Hi ,

 

In this code if i want to delete some empty rows i am not able to do so , I get error to fil the required fields.

Is there any workaround for this.

AlanLamAlanLam

@ fantastic example and works like a treat.

 

Any ideas of the test class that would fit this example please?

 

Thanks in advance. Have a good weekend