• Shaun Bailey 5
  • 0 Points
  • Member since 2016

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
  • 0
You guys rock!  Hopefully, you can help me with a simple Apex Class and VF page that I'm having trouble getting to work.

It works fine when i try to create one record, but the point of this page is to create multiple records.  Here is the error I am receiving:
Insert failed. First exception on row 0 with id a00410000029Ep0AAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!saveHours}' in component <apex:commandButton> in page addmultiplehours2: Class.AddmultipleHours.saveHours: line 21, column 1

An unexpected error has occurred. Your development organization has been notified.

My Apex Class:
public class AddmultipleHours {
 Hours__c hrs = new Hours__c();

 public list < Hours__c > listHours {

 public AddmultipleHours() {
  listHours = new list < Hours__c > ();

 public void addHoursRow() {
  Hours__c hours = new Hours__c();
 public PageReference saveHours() {
  for (Integer i = 0; i < listHours.size(); i++) {
   insert listHours;}
  return Page.AddmultipleHours;


My VF Page:
<apex:page Controller="AddmultipleHours">
  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockTable value="{!listHours}" var="hours">
        <apex:column headerValue="Client Name">
          <apex:inputField value="{!hours.Client_Name__c}"/>
        <apex:column headerValue="Hours">
          <apex:inputField value="{!hours.Hours__c}"/>
        <apex:column headerValue="Type">
          <apex:inputField value="{!hours.Type__c}"/>
      <apex:pageBlockButtons >
        <apex:commandButton value="Add Hours Row" action="{!addHoursRow}"/>
        <apex:commandButton value="Save Hours" action="{!saveHours}"/>

Your help is much appreciated!!