+ Start a Discussion

First error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

I'm tyring to insert a master record and detail record from one visualforce page, but I can't get the test class to work, any idea?


I'm only getting 64% coverage and it's throwing this message.  Thanks in advance!


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

Class.newVendorPriceController.save: line 43, column 7 Class.TestNewVendorPriceController.TestNewVendorPriceController: line 41, column 1 External entry point


public class newVendorPriceController {
   Vendor_Price_Line_Item__c vendorlineitem;
   Vendor_Pricing__c vendorprice;
   Order__c order;
    private final Map<String, String> currentpageParams;

     public newVendorPriceController(ApexPages.StandardController controller) {
     this.vendorprice = (Vendor_Pricing__c)controller.getRecord();
     this.currentpageParams = ApexPages.currentPage().getParameters();

   public Vendor_Pricing__c getvendorprice() {
      if(vendorprice == null) vendorprice = new Vendor_Pricing__c();
      return vendorprice;
     public Vendor_Price_Line_Item__c getvendorlineitem() {
      if(vendorlineitem == null) vendorlineitem = new Vendor_Price_Line_Item__c();
      return vendorlineitem;

    public string oId = apexpages.currentpage().getParameters().get('id');
    public PageReference cancel() {
          PageReference pr = new PageReference('/ui/desktop/DesktopPage');
          return pr;

      public PageReference save() {

      insert vendorprice;
      insert vendorlineitem;

      PageReference vendyPage = new ApexPages.StandardController(vendorprice).view();

      return vendyPage;







Test Method:



private class TestNewVendorPriceController {

static testMethod void TestNewVendorPriceController(){

   Vendor_Pricing__c venprice;
   ApexPages.StandardController sc;
   newVendorPriceController vpc;

Account account = new Account();// specify all the required fields
account.name = 'testing';
account.shippingcity = 'baltimore';
account.shippingstate = 'MD';
account.shippingcountry = 'USA';
account.shippingpostalcode = '21228';
insert account;
Contact contact = new Contact();// specify all the required fields
contact.lastname = 'test';
insert contact;
States__c state = new States__c ();
state.State_Code__c = 'BB';
insert state;
Order__c ord = new Order__c ();
ord.Account__c = account.Id;
insert ord;
Vendor_Pricing__c ven = new Vendor_Pricing__c ();
ven.Order__c = ord.Id;
insert ven;
Vendor_Price_Line_Item__c venline = new Vendor_Price_Line_Item__c ();
venline.Vendor_Price__c = ven.Id;
insert venline;

System.assertNotEquals(null, ven.Id);//tests to see if your insert was successful
   vpc = new newVendorPriceController(new ApexPages.StandardController(ven)); //here you instantiate the controller with a standard controller.
   //start you user testing.

vpc.getvendorlineitem ();



Best Answer chosen by Admin (Salesforce Developers) 

All I did was change the insert to upsert on the Save method.

All Answers


All I did was change the insert to upsert on the Save method.

This was selected as the best answer

Great ! Thank you for your solution. We have been going at this for a few hours now.


The scenario we had was that we had a quote, with 1 or more vehicles attached with 1 or more accessories. If we cloned a quote with one vehicle it worked perfectly, however when we cloned a quote with multiple vehicles it had failed.


we had 2 for loops : 1 for the models and the other for the accessories. for the list we had them both as insert list; we changed the Models list to an upsert list and this has solved our issue.


Thanks alot for your reply it has definitely helped us.