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
JeffroJeffro 

Building Page to Insert Multiple Contacts

I would like to build a Visualforce page where my users can go and insert multiple Contacts in one shot. It would basically act like an Excel sheet with columns like First Name, Last Name, Email, etc. Once they enter all of their Contacts, they would click a button, all Contacts would be inserted.

 

A good scenario would be collecting 20 business cards from a fair. When you get back from the fair you can enter the information from the 20 cards at once, without clicking 'New' 20 individual times. After the data is entered, click 'Insert' and you just created 20 Contacts.

 

Does anyone know if creating repeating records/fields is possible?

 

Thanks,

Jeff

vhanson222vhanson222

You can do this by creating an array of new contacts in your controller and using a repeater to display each contacts information.  A basic example is below. 

 

 

/***** CONTROLLER *****/

public with sharing class MassContactController {
    
    public List<Contact> Contacts { get; set; }
    public List<Contact> ContactsToInsert { get; set; }
    
    public MassContactController() {
        contacts = new List<Contact>();
        
        for (integer i = 0; i < 10; i++)
        {
            Contact newContact = new Contact();
            contacts.add(newContact);
        }
    }
    
    public PageReference Save() {
    
        for (Contact c : Contacts)
        {
            if (isValid(c))
                ContactsToInsert.add(c);
        }
        
        if (ContactsToInsert.size() > 0)
            insert ContactsToInsert;
        
        return null;
    }
    
    public boolean isValid(Contact c) {
        boolean retVal = false;
        
        if (c.LastName != null && c.Phone != null)
            retVal = true;
        
        return retVal;
    }

}




/***** VISUALFORCE PAGE *****/

<apex:page controller="MassContactController">
    <apex:form >
        <table border="0" >
            <tr>
                <th>First Name</th><th>Last name</th>
                <th>Phone</th><th>Email</th>
            </tr>
            <apex:repeat var="contact" value="{!Contacts}">
                <tr>
                    <td><apex:inputField value="{!contact.FirstName}" /></td>
                    <td><apex:inputField value="{!contact.LastName}" /></td>
                    <td><apex:inputField value="{!contact.Phone}" /></td>
                    <td><apex:inputField value="{!contact.Email}" /></td>
                </tr>
            </apex:repeat> 
        </table>
        <apex:commandButton action="{!Save}" title="Save" value="Save"/>
    </apex:form>
</apex:page>

 

 

 

I hope that helps get you started

 

Victor

Shashikant SharmaShashikant Sharma

Controller  : Change private Integer noOfContacts = 20; number as your req

 

 

public class multipleContacts {

   public List<contact> listCon {get;set;}
   private Integer noOfContacts = 20;
   public List<Integer> listIndex {get;set;}
   public multipleContacts ()
       {
          listIndex = new List<integer>();
          listCon  = new List<contact>();
          for(Integer i = 0 ; i < noOfContacts ; i++)
              {
                  listIndex.add(i);
                  listCon.add(new Contact());
              }
          
       }
       
   public PageReference save()
       {
           insert listCon;
           PageReference pg = ApexPages.currentPage();
           pg.setredirect(true);
           return pg;
           
       }    
}

 VFP : 

Step 1 : Create field set with name "MultipleContact" on contact object, add field that you want on screen in "In the Field Set" ,  For creating field set : Customize -> Contact -> Field Sets 

read this for field sets : http://forceschool.blogspot.com/2011/06/dynamic-binding-using-field-sets.html

 

 

 

 

<apex:page controller="multipleContacts">

    <apex:form>
    <apex:pageBlock>
        <apex:pageBlockButtons>
        <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
        <apex:repeat value="{!listIndex}" var="index">
            <apex:pageBlockSection title="Contact Information">
            
                <apex:repeat value="{!$ObjectType.Contact.FieldSets.MultipleContact}" var="field">
                    <apex:inputField value="{!listCon[index][field]}" required="{!field.required}"/>
                </apex:repeat>
                
            </apex:pageBlockSection>
        </apex:repeat>
    
    </apex:pageBlock>
    </apex:form>
      
</apex:page>

 

 

 

Your are all set to get input for multiple contacts.

JeffroJeffro

That is awesome. Thanks for your help.

 

One step further...is there a way to make the number of Contacts a variable? The one issue is that if I specify 20 Contacts but there are really only 15, you will get an error because all of the Last Name fields are required.

 

It would be nice to either pass the number of Contacts as a variable and/or include a button to add another row. Is this possible?

 

Thanks,

Jeff

vhanson222vhanson222

in the button, do Immediate="true", which bypasses the initial validation.

brielea1984brielea1984

I realize this was over a year ago...but I copied this code and it looks great. However, I have no idea how to create a button that goes to the multiplecontacts page. Nowhere on any object does it allow me to override with this page. I've only ever used standard controllers. Advice?

 

I have a custom object called Households__c, and I want to create a button that goes to this page that allows for entering multiole contacts. And I want to autpopulate the Household_ID__c field on the contact page.

 

Any help would be great.

 

Thanks!