You need to sign in to do that
Don't have an account?
jeevitha 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();
}
}
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();
}
}
// 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......!
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();
}
}
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();
}
}
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.
the underlined part has to be your list, which is newInv1
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.
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.