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
bozotheclownbozotheclown 

Multiple Object Record Creation on a Single Page?

Hello.  I have been trying to figure out if there is a simple way in VF that provides the ability for the end user to create multiple new records of a specific custom object on one single page.

 

The below example lists my code that creates a single "Customer__c" record (three fields on a single line). My goal here is to figure out if there is a simple way to be able to enter 5 to 10 new "Customer__c" records on the same page...and just  click the "save" button once to save all of the records.

 

Does anyone know if this is possible...and, if so, could provide some guidance?

 

Thanks in advance.  Below is my code for the single record entry.

 

 

<apex:page standardController="Customer__c" recordSetVar="cust" showheader="false">

<apex:form >
<apex:pageBlock title="Add New Customer">
<apex:pageBlockSection columns="3">
<apex:inputField value="{!Customer__c.name}"/>
<apex:inputField value="{!Customer__c.Region__c}"/>
<apex:inputField value="{!Customer__c.Tier__c}"/>
</apex:pageBlockSection>
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlock>

</apex:form>
</apex:page>

raghu123raghu123

Hi  , You can do that.

 

Here it is..

 

 

...............VF Page...........



<apex:page controller="xyz"   recordSetVar="cust" showheader="false">

<apex:form >
<apex:pageBlock title="Add New Customer">


<apex:pageBlockButtons>

<apex:commandButton value="Add Row" action="{!addrow}"/>

<apex:commandButton value="Save" action="{!save}"

</apex:pageBlockButtons>



<apex:pageBlockTable  columns="3" value={!customers}  var="a" >
<apex:inputField value="{!a.name}"/>
<apex:inputField value="{!a.Region__c}"/>
<apex:inputField value="{!aTier__c}"/>
</apex:pageBlockTable>

</apex:pageBlock>

</apex:form>
</apex:page>





..........Controller.......



public class xyz {

public List<Customer__c> customers = new List<Customer__c>();
public List<Customer__c>  getCustomers(){
return customers;
}
public void addrow(){
customers.add(new Customer__c());
}
public PageReference save(){
insert customers;
}
}

  

 

  cheers,

  Raghu

bozotheclownbozotheclown

Raghu-

 

Thanks for the help.  Unfortunately, I am running into an error with the controller code you recommended.  The "insert customers;" line in the controller is triggering the error message "Compile Error: Non-void method might not return a value or might have statement after a return statement."

 

Any thoughts on why this is happening?  Thanks again for the help.

raghu123raghu123

Try This ...

 

public PageReference save(){
insert customers;

return null;

}

bozotheclownbozotheclown

I appreciate the help with this - but unfortunately I am still experiencing some issues.

 

First, the page displays with the two buttons - but the fields are not visible.  When I click the "add row" button, I receive an error message saying.  "Could not resolve the entity from <apex:inputField> value binding '{!a.name}'. inputField can only be used with SObject fields".

 

Any thoughts on why this is happening?  Thanks again for the help.

raghu123raghu123

Hey

          Sorry. Try this code. Its working..

....................VF Page................
<apex:page controller="xyz"  >
<apex:form >
<apex:pageBlock title="Add New Customer">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>

<apex:pageBlockTable columns="3" value="{!customers}"  var="a" >

<apex:column > <apex:inputField value="{!a.name}"/> </apex:column>
<apex:column >  <apex:inputField value="{!a.Region__c}"/> </apex:column>
<apex:column >  <apex:inputField value="{!a.Tier__c}"/> </apex:column>

<apex:facet name="header">            
<apex:commandButton value="Add Row" action="{!addRow}"/> 
</apex:facet>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

...................Controller.....................

public class xyz {

public List<Customer__c> customers = new List<Customer__c>();
public List<Customer__c>  getCustomers(){
return customers;
}
public void addrow(){
customers.add(new Customer__c());
}
public PageReference save(){
insert customers;
customers.clear();
return null;
}
}

                     

              Cheers,

              Raghu

bozotheclownbozotheclown

Perfect.  Thanks so much for the help.