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
veerna551.3942206611176638E12veerna551.3942206611176638E12 

System.DmlException: Insert failed. First exception on row 0 with id 0019000000xjsdrAAA; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

public with sharing class AccAndCons1 {
public list<contactWrapper> Wrappers{get;set;}
private Integer nextIdent=0;
public static Integer addCount {get; set;}
public  Account acc{get;set;}
public string name{get;set;}

public list<contact> cons=new list<contact>();
    public AccAndCons1(ApexPages.StandardController controller) {
    acc=(Account)controller.getRecord();
    
        wrappers=new List<contactWrapper>();
       
        for (Integer idx=0; idx<2; idx++)
      {
        wrappers.add(new ContactWrapper(nextIdent++));
       
      }
    }
   
    public void addRows()
   {
     for (Integer idx=0; idx<addCount; idx++)
   {
      wrappers.add(new ContactWrapper(nextIdent++));
   }
}
public PageReference save1()
{
 
  acc.name=name;
  // database.saveResult srres= database.insert(acc);
  insert acc;
   list<contact> cc=new list<contact>();
  // if(srres.isSuccess()){
  
   
for (ContactWrapper wrap : wrappers)
  {
 
   cons.add(wrap.con);
  
   
    
   }
  
   for(contact c:cons){
   c.AccountId=acc.id;
   cc.add(c);
   }
//} 
  

  upsert cc;

  return null;
  }
public class contactWrapper{
public integer iden{get;set;}
public Contact con{get;set;}
public id AccountId{get;set;}
  public contactWrapper(integer idenent){
    iden=idenent;
    con=new contact();
  }
}
}


test class:

@isTest
public class AccAndCons1_test{
 
   static testMethod void AccAndCons1_testOne(){
  
     Account a=new Account(name='xyz');
     upsert a;
    
     List<contact> con =new List<contact>();
     contact c1=new contact(lastname='sas',AccountId=a.id);
     contact c2=new contact(lastname='sas1',AccountId=a.id);
     con.add(c1);
     con.add(c2);
     upsert con;
     ApexPages.StandardController sc = new ApexPages.StandardController(a);
     AccAndCons1 aa=new AccAndCons1 (sc);
     aa.addRows();
     aa.save1();
    
     }
   }

when i run the test class it shows the error like:

System.DmlException: Insert failed. First exception on row 0 with id 0019000000xjtULAAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Stack Trace :Class.AccAndCons1.save1: line 33, column 1
Class.AccAndCons1_test.AccAndCons1_testOne: line 18, column 1


Caleb SidelCaleb Sidel
In your testMethod you're creating an Account a.

Account a=new Account(name='xyz');
upsert a; 

You pass this account a into your controller 

ApexPages.StandardController sc = new ApexPages.StandardController(a);  <----note this is the 'a' I refer to later, and it was inserted already
AccAndCons1 aa=new AccAndCons1 (sc);

So then in your controller you run the code

acc=(Account)controller.getRecord();

At this point acc has an Id! acc is the SAME record as 'a' from above

In your controller you have a method save1()

it has this code

// database.saveResult srres= database.insert(acc);
  insert acc;
   list<contact> cc=new list<contact>();
  // if(srres.isSuccess()){


Note that you're calling insert on acc, but acc is already the same as a, which was already created via the upsert in your testMethod.

Hope this helps!




veerna551.3942206611176638E12veerna551.3942206611176638E12
hi Caleb Sidel,
  thanks for your reply,

how to write test class for the above controller.
please help me
thanks in advance
Caleb SidelCaleb Sidel
Hi Veerna,
The problem with writting a testMethod "out of the blue" is that without context the test isn't meaningful. Your goal in writting your testMethod is to test some behavior and assert that you get the proper results.
So without knowing the use case for your VF page it's hard to write proper tests. And you should write both positive and negative tests.

It appears that your page is creating an Account, somehow presenting a list of Contacts and then upserting those Contacts. Are you sure you want to create the Account? or are you trying to update an Account with a list of new Contacts? 

You may want to change your controller to upsert the account instead of insert?

Or if you truely are creating an acocunt everytime then simply change your test method to not insert the Account. But the test Method doesn't call System.assert() to actually assert behavior so the test while it may run your code doesn't prove your code is running properly.

@isTest
public class AccAndCons1_test{

   static testMethod void AccAndCons1_testOne(){
 
     Account a=new Account(name='xyz');
     // COMMENT THIS OUT    upsert a;
   
     List<contact> con =new List<contact>();
     contact c1=new contact(lastname='sas',// can't do this if the account has no id... AccountId=a.id);
     contact c2=new contact(lastname='sas1',// can't do this if the account has no id...AccountId=a.id);
     con.add(c1);
     con.add(c2);
     upsert con;
     ApexPages.StandardController sc = new ApexPages.StandardController(a);
     AccAndCons1 aa=new AccAndCons1 (sc);
     aa.addRows();
     aa.save1();
   
     //You should really query the data that was just saved and ensure it was saved properly. Query for the account with name xyz and does it have an id? then query for the two contacts with lastname sas and sas1 and ensure they have an account Id = the account Id for xyz.? For example?

     }
   }