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
bm777bm777 

Table saves only each current custom input record. How to save all records?

I am very new to SalesForce and I need help about the following situation: 
I have a form with some input fields about a custom object called Student, like name, Surname etc. On the same page below I have another form with a table and I want this table to save every single input record from the form above. What I managed to do is when I hit the button Save every current record is being saved in the table and not all records. 

I do not know how to proceed. Any help would be greatly appreciated!

VisualForce code: 

<apex:page controller="StudentController1">
 <apex:form id="form1">
 
  <apex:pageBlock title="Nov Student">
  
    <apex:pageBlockSection columns="1">
      <apex:inputField value="{!Student.Name}"/>
      <apex:inputField value="{!Student.Priimek__c}"/>
      <apex:inputField value="{!Student.Letnik__c}"/>
      <apex:inputField value="{!Student.Naslov__c}"/>
      <apex:inputField value="{!Student.Datum_rojstva__c}"/>
      <apex:inputField value="{!Student.Naziv_fakultete__c}"/>
      <apex:inputField value="{!Student.Studijski_program__c}"/>
      <apex:inputField value="{!Student.Tip_studija__c}"/>
<apex:selectcheckboxes layout="pagedirection">
      <apex:selectOption id="Placnik" itemValue="Samoplacnik" itemLabel="Samoplacnik" />
</apex:selectcheckboxes>

     <apex:pageBlock title="Vsi studenti">
      <apex:pageBlockTable value="{!Student}" var="wrapper" id="wtable" rows="9">
            <apex:column value="{!wrapper.Name}"/>      
            <apex:column value="{!wrapper.Priimek__c}"/>
            <apex:column value="{!wrapper.Naslov__c}"/>
            <apex:column value="{!wrapper.Datum_rojstva__c}"/>      
            <apex:column value="{!wrapper.Naziv_fakultete__c}"/>
            <apex:column value="{!wrapper.Studijski_program__c}"/>
            <apex:column value="{!wrapper.Letnik__c}"/>
            <apex:column value="{!wrapper.Tip_studija__c}"/>
      </apex:pageBlockTable>
     </apex:pageBlock>
 <apex:commandButton action="{!dodajStudent}" value="Save" id="theButton" reRender="wtable"/>
    </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>



APEX code:

public class StudentController1 {

  public boolean showValue {get;set;}
  public string studentName {get;set;}
  public Student__c student {get;set;}
  public StudentController1(){
        student = new Student__c();
    } 
  public pageReference Save(){
        insert student; 
        return null;
   }     
  public void dodajStudent(){
        upsert student;
  }
  
  public void updateFieldVisibility() {
        showValue = true;
    }
}
sunny.sfdcsunny.sfdc
In Controller:
1. Create a list of Student__c object. This list will store the records you have created in table 1.
2. Iterate over that list in table 2 to show the records you created.
3. Put some button (like add) to add the new record created in table 1 to the list of Student__c object.
4. Once you are done creating records. Click on save and insert the list of Student__c records list.

Following example shows the same for Contact object:
Visualforce Page:
<apex:page controller="AddContactsController">
    <apex:form id="form1">
        <apex:pageBlock title="Contact">
            <apex:commandButton action="{!add}" value="Add" rerender="results"/>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!con.FirstName}"/>
                <apex:inputField value="{!con.Lastname}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!contacts}" var="record" id="results">
                <apex:column value="{!record.Firstname}"/>      
                <apex:column value="{!record.Lastname}"/>
            </apex:pageBlockTable>
            <apex:commandButton action="{!saveRecords}" value="Save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AddContactsController {
    public Contact con {get;set;}
    public List<Contact> contacts {get;set;}
    
    public AddContactsController(){
        contacts = new List<Contact>();
        con = new Contact();
    }
    
    public PageReference add(){
        contacts.add(con);
        con = new Contact();
        return null;
    }
    
    public PageReference saveRecords(){
        insert contacts; 
        return null;
    }     
}