I have a VFPage with Account Fields and one Button as "Save&AddContact",

  1. when i click on this button Account record is inserted and current page is navigate to another page which have Contact Fields.
  2. The second Page have a Buttons like "Save" and "Save&New".
  3. when i click on Save&New Button the Current Records is successfully inserted and become child for Account and need to redirect same page.

    This should be process no of times based on requirement. But when i insert second contact i am getting error like "Before Insert or Upsert list must not have two identically equal elements

Error is in expression '{!insertContact}' in component in page accounttocontatdata: Class.AccountData.insertContact: line 20, column 1

here is my code


public class AccountData{
    public Account acclst       {set;get;}
    public contact conlst       {set;get;}
    public List<Contact>cons    {set;get;}
    public AccountData(){
        acclst=new Account();
        conlst=new Contact();
        cons= new List<Contact>();
    public pagereference convert(){
        insert acclst;
        pagereference pg=new pagereference('/apex/AccountToContatData');
        return pg;
    public PageReference insertContact(){

        Account a=[select id,name from account where id=:acclst.id];
        insert Cons;

        PageReference pg1 = ApexPages.currentPage(); 
        pagereference pg=new pagereference('/apex/AccountToContatData');
        return pg;


VFPage 1:

<apex:page controller="AccountData" >
    <apex:sectionHeader title="Account" />
    <apex:form >
        <apex:pageBlock >
       <apex:pageBlockSection title="Acount Details">
           <apex:inputField value="{!acclst.Name}"/>
           <apex:inputField value="{!acclst.phone}"/>
           <apex:inputField value="{!acclst.type}"/>
           <apex:inputField value="{!acclst.ShippingAddress__c}"/>
        <apex:pageBlockSection title="Additional Information">
           <apex:inputField value="{!acclst.BillingCity__c}"/>
            <apex:inputField value="{!acclst.BillingBranch__c}"/>
            <apex:pageBlockButtons >
                <apex:commandButton value="save&AddContact" action="{!convert}"/>


<apex:page controller="AccountData" id="d">
    <apex:sectionHeader title="AccountToContact"/>
    <apex:form >
        <apex:pageBlock id="o" >
            <apex:pageBlockSection columns="2" >
        <apex:inputField value="{!conlst.LastName}"/>
        <apex:inputField value="{!conlst.FirstName}"/>
        <apex:inputField value="{!conlst.Email}"/>
        <apex:inputField value="{!conlst.phone}"/>
        <apex:inputField value="{!conlst.Address__c}"/>
        <apex:inputField value="{!conlst.Department}"/>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="save" />
                <apex:commandButton value="save&New" action="{!insertContact}" reRender="o"/>
                <apex:commandButton value="Home"/>

2 Answers 2


In your controller on line 20 something,


The above line actually flushes the viewstate and hence information that was kept by page1 will not be shared with page2.

Please note the following:

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.


Your insertContact() method, which adds the Contact record to a list to be inserted, is adding the same Contact record to the same list when it is executed a second time. Thus the same contact record appears twice in the list when the insert DML is executed.

I would recommend inserting the Contact directly, not as part of a list, then declaring the Contact as a new record upon successful insert.

This would be removing the following from insertContact()...

insert cons;

... including something like the following instead...

insert conlst;
conlst = new Contact();

By this the Contact record is declared as new, and instead of adding the same contact to a list to be inserted, the Contact is being inserted directly.

You must log in to answer this question.

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