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
Shaun Bailey 5Shaun Bailey 5 

First exception on row 0 with id

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 {
  get;
  set;
 }

 public AddmultipleHours() {
  listHours = new list < Hours__c > ();
  listHours.add(hrs);
 }

 public void addHoursRow() {
  Hours__c hours = new Hours__c();
  listHours.add(hours);
 }
 
 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>
        <apex:column headerValue="Hours">
          <apex:inputField value="{!hours.Hours__c}"/>
        </apex:column>
        <apex:column headerValue="Type">
          <apex:inputField value="{!hours.Type__c}"/>
        </apex:column>
      </apex:pageBlockTable>
      <apex:pageBlockButtons >
        <apex:commandButton value="Add Hours Row" action="{!addHoursRow}"/>
        <apex:commandButton value="Save Hours" action="{!saveHours}"/>
      </apex:pageBlockButtons>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Your help is much appreciated!!
 
AshlekhAshlekh
Hi

The logic which is below is wrong.
 
public PageReference saveHours() {
  for (Integer i = 0; i < listHours.size(); i++) {
   insert listHours;}
   
  return Page.AddmultipleHours;
 }


listHours is type of list and if it has two records then accroding to your logic insert operation will happen two times,

when i = 0, insert listhours will insert all records into database and all records will have id.
when i = 1 , then again your logic will insert same records but this time it will throw an exception as records all ready have id so you can't insert.

Use below logic.

 
public PageReference saveHours() {
  if(listHours.size()>0)
   upsert listHours;
  
  return Page.AddmultipleHours;
 }
 
or
 
public PageReference saveHours()
{
 list < Hours__c > listHourslocal = new list < Hours__c > ();
 for (Integer i = 0; i < listHours.size(); i++) {
    if(listHours[i].id ==null)
    listHourslocal.add(listHours);
 }
  
if(listHourslocal.size()>0)
 insert listHourslocal;
 
 return Page.AddmultipleHours;
}



-Thanks
Ashlekh Gera
 
Shaun B.Shaun B.
Thanks Ashlek.  I applied your code, but I get the following error:
 
Error: Compile Error: Incompatible element type List<Hours__c> for collection of Hours__c at line 18 column 42

Here is the code I'm using:
 
public class AddMultipleHours {
    Hours__c hrs = new Hours__c();
    public list < Hours__c > listHours {
        get;
        set;
    }
    public AddMultipleHours() {
        listHours = new list < Hours__c > ();
        listHours.add(hrs);
    }
    public void addHoursRow() {
        Hours__c hours = new Hours__c();
        listHours.add(hours);
    }
    public PageReference saveHours() {
        list < Hours__c > listHourslocal = new list < Hours__c > ();
        for (Integer i = 0; i < listHours.size(); i++) {
            if (listHours[i].id == null) listHourslocal.add(listHours);
        }
        if (listHourslocal.size() > 0) insert listHourslocal;
        return Page.AddmultipleHours;
    }
}

Any ideas?