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
jeevitha annabathula 5jeevitha annabathula 5 

how to solve this error in test class:INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

@isTest
public class InvoicelineConvertTest
{
    static testmethod void InvoicelineConvertTest ()
    {
        Invoice_Line__c b = new Invoice_Line__c(Quantity__c=100, Unit_Price__c=90,Invoice__c='a0Z0R000002u0KE');
        
   //a0p0R000003TCD2
       System.debug('Unit Price before inserting new Invoice Line: ' + b.Unit_Price__c);

       // Insert Invoice Line
       insert b;
    
       // Retrieve the Invoice Line
       b = [SELECT Unit_Price__c FROM Invoice_Line__c WHERE Id =:b.Id];
       System.debug('Price after trigger fired: ' + b.Unit_Price__c);

       // Test that the trigger correctly updated the price
       System.assertEquals(90, b.Unit_Price__c);
        
        Invoice_Line__c[] newInv1 = [select id,name,Invoice__c from Invoice_Line__c];
        insert newInv1;
        ApexPages.StandardSetController sc = new ApexPages.standardSetController(newInv1);
        InvoiceLineItems  controller = new InvoiceLineItems(sc);
        Test.startTest();
            controller.addLineItem();
            controller.removerow();
            controller.cancel();
         System.assertNotEquals(null,controller.saveLineItem());
        Test.stopTest();
            
        
               
    }
}
Harsh P.Harsh P.
Replace this lines:

    // Retrieve the Invoice Line
       b = [SELECT Unit_Price__c FROM Invoice_Line__c WHERE Id =:b.Id];
       System.debug('Price after trigger fired: ' + b.Unit_Price__c);

       // Test that the trigger correctly updated the price
       System.assertEquals(90, b.Unit_Price__c);

With this lines:

  // Retrieve the Invoice Line
      Invoice_Line__c  bb = [SELECT Unit_Price__c FROM Invoice_Line__c WHERE Id =:b.Id];
       System.debug('Price after trigger fired: ' + bb.Unit_Price__c);

       // Test that the trigger correctly updated the price
       System.assertEquals(90, bb.Unit_Price__c);

        
     Please mark as best answere.
Thank you......!

     
jeevitha annabathula 5jeevitha annabathula 5
Thank you for your prompt reply.
I have replaced the lines but still getting error as:

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

here is my updated code .Please help me with this:
@isTest
public class InvoicelineConvertTest
{
    static testmethod void InvoicelineConvertTest ()
    {
        Invoice_Line__c b = new Invoice_Line__c(Quantity__c=100, Unit_Price__c=90,Invoice__c='a0Z0R000002u0KE');
        
   //a0p0R000003TCD2
       System.debug('Unit Price before inserting new Invoice Line: ' + b.Unit_Price__c);

       // Insert Invoice Line
       insert b;
    
       // Retrieve the Invoice Line       
      Invoice_Line__c  bb = [SELECT Unit_Price__c FROM Invoice_Line__c WHERE Id =:b.Id];
       System.debug('Price after trigger fired: ' + bb.Unit_Price__c);

       // Test that the trigger correctly updated the price
       System.assertEquals(90, bb.Unit_Price__c);
        
        Invoice_Line__c[] newInv1 = [select name,Invoice__c from Invoice_Line__c where Invoice__c='a0Z0R000002u0KE' ];
        insert newInv1;
        ApexPages.StandardSetController sc = new ApexPages.standardSetController(newInv1);
        InvoiceLineItems  controller = new InvoiceLineItems(sc);
        Test.startTest();
            controller.addLineItem();
            controller.removerow();
            controller.cancel();
         System.assertNotEquals(null,controller.saveLineItem());
        Test.stopTest();
            
        
               
    }
}
DimondDimond
insert newInv1 is your problem. newInv1 has an id prop set to the record that was returned. 
jeevitha annabathula 5jeevitha annabathula 5
Hi I am new to coding. May I know how to solve this error.
Here is my apex class:
public with sharing class InvoiceLineItems {
   public List<Invoice_Line__c> listMeetingAttendee {get; set;}
    Invoice_Line__c meetingAttendee = new Invoice_Line__c();
    Invoice__c meetingAttendees = new Invoice__c();
    public String KARId = apexpages.currentpage().getParameters().get('Id');
     public ApexPages.StandardSetController stdCntrlr {get; set;}
     public InvoiceLineItems(ApexPages.StandardSetController controller) {
        stdCntrlr = controller;
          listMeetingAttendee = new List<Invoice_Line__c>();
        system.debug(KARId);
        if(KARId<>null){
            for(Invoice__c KAR: [SELECT Id FROM Invoice__c WHERE Id =:KARId ]){
                meetingAttendee.Invoice__c = KARId ;
                listMeetingAttendee.add(meetingAttendee );
            }      
        } else{
             listMeetingAttendee.add(meetingAttendee );
        }
        
    }
    public void addLineItem(){
        Invoice_Line__c meetAtt = new Invoice_Line__c();
        meetAtt.Invoice__c = KARId ;
        listMeetingAttendee.add(meetAtt);
    }
    public PageReference saveLineItem() {
         system.debug('listMeetingAttendee'+listMeetingAttendee);
        for(Integer i=0; i<listMeetingAttendee.size(); i++)
        {
            upsert listMeetingAttendee;
        }
        PageReference nextPage = new ApexPages.StandardController(meetingAttendees).view();
        nextPage.setRedirect(true);      
         return nextPage;
        }
    public void removerow()
     {
         Integer i = listMeetingAttendee.size();
          listMeetingAttendee.remove(i-1);
     }
         public PageReference cancel(){
         Id ide =  ApexPages.Currentpage().getParameters().get('id');
         return new PageReference('/' + KARId );
    }
}

This is my test Class:
@isTest
public class InvoicelineConvertTest
{
    static testmethod void InvoicelineConvertTest ()
    {
        Invoice_Line__c b = new Invoice_Line__c(Quantity__c=100, Unit_Price__c=90,Invoice__c='a0Z0R000002u0KE');
        
   //a0p0R000003TCD2
       System.debug('Unit Price before inserting new Invoice Line: ' + b.Unit_Price__c);

       // Insert Invoice Line
       insert b;
    
       // Retrieve the Invoice Line       
      Invoice_Line__c  bb = [SELECT Unit_Price__c FROM Invoice_Line__c WHERE Id =:b.Id];
       System.debug('Price after trigger fired: ' + bb.Unit_Price__c);

       // Test that the trigger correctly updated the price
       System.assertEquals(90, bb.Unit_Price__c);
        
        Invoice_Line__c[] newInv1 = [select name,Invoice__c from Invoice_Line__c where Invoice__c='a0Z0R000002u0KE' ];
        insert newInv1;
        ApexPages.StandardSetController sc = new ApexPages.standardSetController(newInv1);
        InvoiceLineItems  controller = new InvoiceLineItems(sc);
        Test.startTest();
            controller.addLineItem();
            controller.removerow();
            controller.cancel();
         System.assertNotEquals(null,controller.saveLineItem());
        Test.stopTest();
            
        
               
    }
}
Anant KamatAnant Kamat
You cannot assign the data retrieved to newInv1 and insert it directly.
First you need to iterate the list as below.
Invoice_Line__c[] lstNewInv = new Invoice_Line__c[];

for(Invoice_Line__c inv :  Invoice_Line__c[]){
      Invoice_Line__c i = new Invoice_Line__c(Name = inv.Name, Invoice__c = inv.Invoice__c);
      lstNewInv.add(i);
}

insert lstNewInv;

Let me know if it works as expected.
 
jeevitha annabathula 5jeevitha annabathula 5
Thank you . But it is throwing errors as so I modified code as below Invoice_Line__c[] lstNewInv = new Invoice_Line__c[]{}; for(Invoice_Line__c inv : Invoice_Line__c()){ Invoice_Line__c i = new Invoice_Line__c(Invoice__c = inv.Invoice__c); lstNewInv.add(i); } insert lstNewInv; now it is showing error as "Method does not exist or incorrect signature: void Invoice_Line__c() from the type InvoicelineConvertTest". Please help me with this.
DimondDimond
Invoice_Line__c[] lstNewInv = new Invoice_Line__c[]{}; for(Invoice_Line__c inv : Invoice_Line__c()){ Invoice_Line__c i = new Invoice_Line__c(Invoice__c = inv.Invoice__c); lstNewInv.add(i); }

the underlined part has to be your list, which is newInv1
Anant KamatAnant Kamat
Sorry my bad, as Dimond mentioned it is newInv1 and not Invoice_Line__c[]. Not sure how I missed it.

Invoice_Line__c[] lstNewInv = new Invoice_Line__c[];

for(Invoice_Line__c inv :  newInv1){
      Invoice_Line__c i = new Invoice_Line__c(Name = inv.Name, Invoice__c = inv.Invoice__c);
      lstNewInv.add(i);
}

insert lstNewInv;

Let me know if it is giving the same error.
jeevitha annabathula 5jeevitha annabathula 5
Hi Anant,
Thank you for you reply.
I have solved the using by using following code:
@isTest
public class InvoicelineConvertTest
{
    static testMethod void testExtensionTest()
    {
        Invoice__c exp = new Invoice__c();
        exp.Bill_to_Name__c= 'a0c0R000002HRCj';
        exp.Vendor_Name__c= 'a0W0R000000iS16';
        // Add all required field here
        insert exp;
    
        Invoice_Line__c  eli = new Invoice_Line__c ();
        eli.Invoice__c = exp.id;
        // add all required field
        insert eli;
        List<Invoice_Line__c> yourList = new List<Invoice_Line__c>();
       yourList.add(eli);
        Test.StartTest();
        
            ApexPages.currentPage().getParameters().put('Id', String.valueOf(exp.Id));
            ApexPages.StandardSetController sc = new ApexPages.StandardSetController(yourList);
            InvoiceLineItems obj = new InvoiceLineItems(sc);
            obj.addLineItem();
            obj.saveLineItem();
            obj.removerow();
            obj.cancel();                        
        Test.StopTest();
    }
}

Thank you all for helping me in solving the issue.