<apex:page Controller="AddmultipleAccountsController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listAccount}" var="acc">
<apex:column headerValue="Account Name">
<apex:inputField value="{!acc.Name}"/>
<apex:column headerValue="Account Number">
<apex:inputField value="{!acc.AccountNumber}"/>
<apex:column headerValue="Account Type">
<apex:inputField value="{!acc.Type}"/>
<apex:column headerValue="Industry">
<apex:inputField value="{!acc.Industry}"/>
<apex:pageBlockButtons >
<apex:commandButton value="Add Accounts Row" action="{!addAccount}"/>
<apex:commandButton value="Save Accounts" action="{!saveAccount}"/>


public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }

public AddmultipleAccountsController()
listAccount=new list<Account>();

Public void addAccount()
Account acc = new Account();
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
insert listAccount;
return Page.Allaccountssaved;

I need to add multiple accounts in a page and save it. adding multiple rows is working fine. But when i try to add multiple accounts and click on "save" i am getting an exception.

System.DmlException: Insert failed. First exception on row 0 with id 0019000001EJyEsAAL; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Error is in expression '{!saveAccount}' in component in page addmultipleaccountscontroller: Class.AddmultipleAccountsController.saveAccount: line 19, column 1

Class.AddmultipleAccountsController.saveAccount: line 19, column 1

when i am adding 1 record it is inserted succesfful, but multiple records by clciking on add and save i am getting exception

that is while inserting can any one help me out. Thanks in advance.

1 Answer 1


Simply changing your insert to upsert will solve the problem. When you redirect (presumably to the same page), your view state is left intact by default. Since the prior account already has an ID, Salesforce is reminding you that you're trying to perform an update on the existing account, because inserts can never specify an ID, and an update must always specify an ID (but upsert can perform inserts and updates intelligently).

  • Thanks for the quick response Sfdfox, it is working fine now
    – swa
    Jan 28, 2015 at 7:16
  • Hi i want to add remove functionality in same table i am new to salesforce can u pls help me out sfdcfox
    – swa
    Jan 28, 2015 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .