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

How to create multiple records from one VF page

I have a custom object that is a child of another.  The child lets the user say how to split up the items in the parent.  Each child contains a lookup to the parent with a number and a description.  I can easily do this by having the user create separate child records.  But the client wants to be able to enter 5 at a time.  I could put 4 more number and 4 more description fields but for editing purposes later, it would be better to create separate records in salesforce.  


Since I only have 2 fields in the object how do I get input for 10?  inputField only works for the first set. 


Thanks for any suggestions,



You can add a data table to your VF page and expose a List<> of your child objects in your apex controller or extension. Here's an example of the datatable:


<apex:dataTable value="{!ActivitiesList}" var="field" cellpadding="3px" > <apex:column value="{!field.Step__c}" /> <apex:column > <apex:inputField value="{!field.Name}" /> </apex:column> <apex:column > <apex:inputText value="{!field.Comments__c}"/> </apex:column> </apex:dataTable>

Your child object list in this case is exposed by the ActivitiesList property. In this case the child object has Step, Name, and Comments. The Name and comments are editable, and Step value is read only.




Thanks, but if I have 3 children, that displays the 3 of them.  Perhaps I wasn't clear.  The client wants to be able to add 5 children at a time.  Your code displays all existing childen and lets them to edit - which is also helpful, but how do I display 5 blank lines to get the data in?



Sorry I am fairly new to this..  So I can display the ones that are there and follow them by a number of <apex:inputText...> I guess I will just name them field 1 - 5 and then in the controller split them apart into separate records.

When you create the list of objects in the controller, simply add in new (empty) instances up to the amount that you wish the user to enter.  Then each row in the table is bound to either an existing instance that you update or a new instance that you insert.


Not sure how to add blank ones.  In the Controller I have



List<Ship_Packages__c> packages;


public List<Ship+Packages__c> get packages(){

if (packages==null) packages = [select name, dept__c, num_in_pkg__c from Ship_Packages__c];

return packages;



That returns all the records that exist, but how do I add blank ones?




List<Ship_Packages__c> packages; public List<Ship+Packages__c> get packages(){ if (packages==null) { packages = [select name, dept__c, num_in_pkg__c from ship_Packages__c]; while (packages.Size() < 5) packages.Add(new ship_Packages__c()); } return packages; }



Sorry, the accessor method should be named getPackages();


Cool. Thanks.  I will try that..