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
Waqar Hussain SFWaqar Hussain SF 

how to add a new row in vf page. I am not getting properly. what is missing?

This is my page..

<apex:page standardController="Employee__c" extensions="recordExt">
  <apex:form>
 
    <apex:pageBlock title="Employee Records">
    <apex:messages/>
   
    <apex:pageBlockButtons>
    <apex:commandButton value="Save" action="{!mySave}"/>
    <apex:commandButton value="Cancel"/>
    </apex:pageBlockButtons>
   
    <apex:pageBlockSection>
    <apex:pageBlockTable value="{!employee__c}" var="e" >
      <apex:column headerValue="Employee Name">
        <apex:inputField value="{!e.Name}"/>
      </apex:column>
     
      <apex:column headerValue="Last Name">
        <apex:inputField value="{!e.Last_Name__c}"/>
      </apex:column>
     
      <apex:column headerValue="Joining Date">
        <apex:inputField value="{!e.Join_Date__c}"/>
      </apex:column>
     
      <apex:column headerValue="City">
        <apex:inputField value="{!e.City__c}"/>
      </apex:column>
   
      <apex:column headerValue="Phone">
        <apex:inputField value="{!e.phone__c}"/>
      </apex:column>
   
      <apex:column>
        <apex:commandButton value="+" action="{!add}"/>
      </apex:column>
     
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>


and here is my controller.
public class recordExt {

    public Employee__c emp {get; set;}
    public List<Employee__c> empList {get; set;}
    public static Integer addCount {get; set;}
   
    public recordExt(ApexPages.StandardController controller) {
    this.emp = (Employee__c)Controller.getRecord();
    empList = new List<employee__c>();
    }
    
    public void add() {
       empList.add(new employee__c());
      //this.empList = new List<employee__c>();
     }
    
    public pageReference mySave() {
    try{
        insert emp;
        }
    catch(Exception ex){
            ApexPages.addmessages(ex);
        }
        return null;
   }
   
}
Best Answer chosen by Waqar Hussain SF
CheyneCheyne
The value for your apexPageBlockTable tag should reference the list of employees, empList, in your controller. Your add method looks good,but your save method should do an upsert on the employee list, instead of on a single employee. Then, if you add a rerender to the add button on the visualforce page, it can rerender the table when you add an employee, so that it shows the newest list. You'll probably want a rerender attribute on the save button as well. I'm not sure if this is exactly what you're looking for, but hopefully this will set you on the right track.

<apex:page standardController="Employee__c" extensions="recordExt">
  <apex:form>

    <apex:pageBlock title="Employee Records">
    <apex:messages/>
  
    <apex:pageBlockButtons>
    <apex:commandButton value="Save" action="{!mySave}" rerender="employeeSection"/>
    <apex:commandButton value="Cancel"/>
    </apex:pageBlockButtons>
  
    <apex:pageBlockSection id="employeeSection">
    <apex:pageBlockTable value="{!empList}" var="e" >
      <apex:column headerValue="Employee Name">
        <apex:inputField value="{!e.Name}"/>
      </apex:column>
    
      <apex:column headerValue="Last Name">
        <apex:inputField value="{!e.Last_Name__c}"/>
      </apex:column>
    
      <apex:column headerValue="Joining Date">
        <apex:inputField value="{!e.Join_Date__c}"/>
      </apex:column>
    
      <apex:column headerValue="City">
        <apex:inputField value="{!e.City__c}"/>
      </apex:column>
  
      <apex:column headerValue="Phone">
        <apex:inputField value="{!e.phone__c}"/>
      </apex:column>
  
      <apex:column>
        <apex:commandButton value="+" action="{!add}" rerender="employeeSection"/>
      </apex:column>
    
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>


public class recordExt {

    public Employee__c emp {get; set;}
    public List<Employee__c> empList {get; set;}
    public static Integer addCount {get; set;}
  
    public recordExt(ApexPages.StandardController controller) {
    this.emp = (Employee__c)Controller.getRecord();
    empList = new List<employee__c>();
    }
   
    public void add() {
       empList.add(new employee__c());
      //this.empList = new List<employee__c>();
     }
   
    public pageReference mySave() {
    try{
        upsert empList;
        }
    catch(Exception ex){
            ApexPages.addmessages(ex);
        }
        return null;
   }
  
}

All Answers

srlawr uksrlawr uk
you're going to tell us what is going wrong I'm afraid!! Do you have a screen shot or an error message to hand?
srlawr uksrlawr uk
ok.. it looks like you are trying to build a page on which employee data can be added, in a row, and new rows can be added (thus allowing a user to "input" many employees at a time) and then be able to save said list of employees?? Is that right?

Thought I am aware of the pitfalls you may be facing, and can maybe see some nuances in your code I might investigate, to save us all time, if you can give a specific problem - I'm sure we will be happy to review it with you!
CheyneCheyne
The value for your apexPageBlockTable tag should reference the list of employees, empList, in your controller. Your add method looks good,but your save method should do an upsert on the employee list, instead of on a single employee. Then, if you add a rerender to the add button on the visualforce page, it can rerender the table when you add an employee, so that it shows the newest list. You'll probably want a rerender attribute on the save button as well. I'm not sure if this is exactly what you're looking for, but hopefully this will set you on the right track.

<apex:page standardController="Employee__c" extensions="recordExt">
  <apex:form>

    <apex:pageBlock title="Employee Records">
    <apex:messages/>
  
    <apex:pageBlockButtons>
    <apex:commandButton value="Save" action="{!mySave}" rerender="employeeSection"/>
    <apex:commandButton value="Cancel"/>
    </apex:pageBlockButtons>
  
    <apex:pageBlockSection id="employeeSection">
    <apex:pageBlockTable value="{!empList}" var="e" >
      <apex:column headerValue="Employee Name">
        <apex:inputField value="{!e.Name}"/>
      </apex:column>
    
      <apex:column headerValue="Last Name">
        <apex:inputField value="{!e.Last_Name__c}"/>
      </apex:column>
    
      <apex:column headerValue="Joining Date">
        <apex:inputField value="{!e.Join_Date__c}"/>
      </apex:column>
    
      <apex:column headerValue="City">
        <apex:inputField value="{!e.City__c}"/>
      </apex:column>
  
      <apex:column headerValue="Phone">
        <apex:inputField value="{!e.phone__c}"/>
      </apex:column>
  
      <apex:column>
        <apex:commandButton value="+" action="{!add}" rerender="employeeSection"/>
      </apex:column>
    
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>


public class recordExt {

    public Employee__c emp {get; set;}
    public List<Employee__c> empList {get; set;}
    public static Integer addCount {get; set;}
  
    public recordExt(ApexPages.StandardController controller) {
    this.emp = (Employee__c)Controller.getRecord();
    empList = new List<employee__c>();
    }
   
    public void add() {
       empList.add(new employee__c());
      //this.empList = new List<employee__c>();
     }
   
    public pageReference mySave() {
    try{
        upsert empList;
        }
    catch(Exception ex){
            ApexPages.addmessages(ex);
        }
        return null;
   }
  
}
This was selected as the best answer