You need to sign in to do that
Don't have an account?

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Error
This is my Test Method:
@isTest
private class ContractMasterControllerTestClass2{
static testMethod void contractMastermethod(){
ApexPages.currentPage().getParameters().put('id', '800O00000000Kp4');
ContractMasterController ct = new ContractMasterController();
List<ContractMaster__c> c = ct.getContractMasterDet();
List<ContractMaster__c> t = ct.getContractMasterreports();
ContractMaster__c conmaster = [SELECT Specification_on_dry_basis__c,Fines__c,Lumps__c,UltraFines__c from
ContractMaster__c where Name=:'000153'];
/*List<ContractMaster__c> cupdate=new List<ContractMaster__c>();
List<ContractMaster__c> cinsert=new List<ContractMaster__c>();
update cupdate;
insert cinsert;*/
// Retrieve account
ct.coninsert= [SELECT Status, Seller__c,
Buyer__c,
Account.Name,
ContractNo__c,
Commodity__c,
FinesQtyperyear__c,
LumpsQtyperyear__c,
UltraFinesQtyPerYear__c,
AdditionalQtyifany__c,
EndDate,
ContractTerm,
StartDate,Fines__c,Lumps__c,UltraFines__c
FROM Contract WHERE Id =:'800O00000000Kp4IAE'];
Date dat = Date.today();
Contract newcon = new Contract();
newcon.AccountID='001O0000001nmuG';
newcon.Name='TCS';
newcon.Status='Draft';
newcon.StartDate=dat;
newcon.ContractTerm=15;
insert newcon;
// ct.setconinsert(newcon);
PageReference pref1 = ct.MyUpdate();
update newcon;
delete newcon;
PageReference pref2 = ct.MyEdit();
PageReference pref3 = ct.Cancel();
PageReference pref4=ct.MYSave();
System.debug(pref4);
}
I got the Following Error:
System.DmlException: Insert failed. First exception on row 0 with id 800O00000000Kp4IAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Could you Please Provide me the solution for this.
}
}
Nothing jumps out as obvious. Are you doing an insert somewhere beneath this method?
PageReference pref1 = ct.MyUpdate();
Because it looks like you are effectively setting an id on a contract object, named "coninsert" incidentally, in the ct object with this statement:
ct.coninsert= [SELECT Status, Seller__c,
Buyer__c,
Account.Name,
ContractNo__c,
Commodity__c,
FinesQtyperyear__c,
LumpsQtyperyear__c,
UltraFinesQtyPerYear__c,
AdditionalQtyifany__c,
EndDate,
ContractTerm,
StartDate,Fines__c,Lumps__c,UltraFines__c
FROM Contract WHERE Id =:'800O00000000Kp4IAE'];