You need to sign in to do that
Don't have an account?
Rutvij Pathak 5
INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call for multiple row entries
Getting error for multiple inputs in a row. Could not get an exact issue, Can someone help?
Insert failed. First exception on row 0 with id 0036F000023Ra8GQAS; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!save}' in component <apex:commandButton> in page createmultiplecontacts: Class.AddMultipleContacts.save: line 27, column 1
****************************************************************************************************
public class AddMultipleContacts {
public List<Contact> ContactList {get;set;}
public Integer rowNum{get;set;}
public string AcctID{get;set;}
sObject con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
public AddMultipleContacts(ApexPages.StandardController controller ){
AcctID = ApexPages.currentPage().getParameters().get('Id');
system.debug('-AcctID--'+AcctID);
ContactList = new List<Contact>();
ContactList.add(new contact(Accountid = Acctid));
}
public PageReference save(){
for(Contact con :ContactList)
{
if(con.FirstName == '' || con.FirstName == null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
return null;
} else
{
insert ContactList ;
}
}
PageReference page1 = new PageReference ('/'+ AcctId );
Page1.setRedirect(true) ;
return page1;
}
public void insertRow(){
ContactList.add(new contact(Accountid = Acctid ));
}
public void delRow(){
rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
ContactList.remove(rowNum);
}
public PageReference Cancel(){
PageReference page2 = new PageReference ('/'+ AcctId ) ;
Page2.setRedirect(true) ;
return page2;
}
}
Insert failed. First exception on row 0 with id 0036F000023Ra8GQAS; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!save}' in component <apex:commandButton> in page createmultiplecontacts: Class.AddMultipleContacts.save: line 27, column 1
****************************************************************************************************
public class AddMultipleContacts {
public List<Contact> ContactList {get;set;}
public Integer rowNum{get;set;}
public string AcctID{get;set;}
sObject con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
public AddMultipleContacts(ApexPages.StandardController controller ){
AcctID = ApexPages.currentPage().getParameters().get('Id');
system.debug('-AcctID--'+AcctID);
ContactList = new List<Contact>();
ContactList.add(new contact(Accountid = Acctid));
}
public PageReference save(){
for(Contact con :ContactList)
{
if(con.FirstName == '' || con.FirstName == null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
return null;
} else
{
insert ContactList ;
}
}
PageReference page1 = new PageReference ('/'+ AcctId );
Page1.setRedirect(true) ;
return page1;
}
public void insertRow(){
ContactList.add(new contact(Accountid = Acctid ));
}
public void delRow(){
rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
ContactList.remove(rowNum);
}
public PageReference Cancel(){
PageReference page2 = new PageReference ('/'+ AcctId ) ;
Page2.setRedirect(true) ;
return page2;
}
}
based on this: you loop the list, then insert the list, then insert the list again, then insert again... and so on...
therefore, the inserted data was re-inserted.
you should avoid DML inside the loop:
but to fix the syntax only: (but this is not suggested because there is a governor limit)
The contact's LastName and Firstname is not getting set
try this code
Hope it helps
RD
Try this, tested and working for me
Regards
RD