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
Hi,
Can someone help, I keep getting the error:
System.DmlException: Insert failed. First exception on row 0 with id 00oS0000000AOgYIAW; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
But I am pretty sure that there is no Id specified in my insert call.
Here is my code:
public PageReference save() { List<OpportunityLineItemSchedule> revenueSchedulesToUpdate = new List<OpportunityLineItemSchedule>(); List<OpportunityLineItemSchedule> revenueSchedulesToInsert = new List<OpportunityLineItemSchedule>(); for(revenueSchedulesDate revenueSchedulesDate:revenueSchedulesDates){ for(OpportunityLineItemSchedule revenueSchedule:revenueSchedulesDate.getRevenueSchedules()){ if(revenueSchedule.get('Id') == null) revenueSchedulesToInsert.add(revenueSchedule); else revenueSchedulesToUpdate.add(revenueSchedule); } if(revenueSchedulesToUpdate.size() > 0) update revenueSchedulesToUpdate; if(revenueSchedulesToInsert.size() > 0) insert revenueSchedulesToInsert; } return Page.revenueScheduleView2; }
Hey
You're getting this error becuase you're adding items to list; inserting the list; adding more items to the list; and then trying to insert all the items you've already inserted and the new ones.
You shouldn't be performing DML inside a loop, although isn't directly causing your issue. Try this instead:
public PageReference save() {
List<OpportunityLineItemSchedule> revenueSchedulesToUpdate = new List<OpportunityLineItemSchedule>();
List<OpportunityLineItemSchedule> revenueSchedulesToInsert = new List<OpportunityLineItemSchedule>();
for(revenueSchedulesDate revenueSchedulesDate:revenueSchedulesDates){
for(OpportunityLineItemSchedule revenueSchedule:revenueSchedulesDate.getRevenueSchedules()){
if(revenueSchedule.get('Id') == null)
revenueSchedulesToInsert.add(revenueSchedule);
else
revenueSchedulesToUpdate.add(revenueSchedule);
}
}
update revenueSchedulesToUpdate;
insert revenueSchedulesToInsert;
return Page.revenueScheduleView2;
}
This will help with governor limits(although this still depends on the amoutn of data you work with), and will avoid your other issue.
Wes
All Answers
Also it works if I change the code to:
public PageReference save() {
List<OpportunityLineItemSchedule> revenueSchedulesToUpdate = new List<OpportunityLineItemSchedule>();
List<OpportunityLineItemSchedule> revenueSchedulesToInsert = new List<OpportunityLineItemSchedule>();
for(revenueSchedulesDate revenueSchedulesDate:revenueSchedulesDates){
for(OpportunityLineItemSchedule revenueSchedule:revenueSchedulesDate.getRevenueSchedules()){
if(revenueSchedule.get('Id') == null) insert revenueSchedule;
else revenueSchedulesToUpdate.add(revenueSchedule);
}
if(revenueSchedulesToUpdate.size() > 0) update revenueSchedulesToUpdate;
//if(revenueSchedulesToInsert.size() > 0) insert revenueSchedulesToInsert;
}
return Page.revenueScheduleView2;
}
but then it will reach the governor limits very quickly...
Hey
You're getting this error becuase you're adding items to list; inserting the list; adding more items to the list; and then trying to insert all the items you've already inserted and the new ones.
You shouldn't be performing DML inside a loop, although isn't directly causing your issue. Try this instead:
public PageReference save() {
List<OpportunityLineItemSchedule> revenueSchedulesToUpdate = new List<OpportunityLineItemSchedule>();
List<OpportunityLineItemSchedule> revenueSchedulesToInsert = new List<OpportunityLineItemSchedule>();
for(revenueSchedulesDate revenueSchedulesDate:revenueSchedulesDates){
for(OpportunityLineItemSchedule revenueSchedule:revenueSchedulesDate.getRevenueSchedules()){
if(revenueSchedule.get('Id') == null)
revenueSchedulesToInsert.add(revenueSchedule);
else
revenueSchedulesToUpdate.add(revenueSchedule);
}
}
update revenueSchedulesToUpdate;
insert revenueSchedulesToInsert;
return Page.revenueScheduleView2;
}
This will help with governor limits(although this still depends on the amoutn of data you work with), and will avoid your other issue.
Wes
public pagereference save()
{
Temp_Booking_Setting__c csTaskValues = [select Candidate_Task_Comments__c,Client_Task_Comments__c,Candidate_Task_Subject__c,Client_Task_Subject__c from Temp_Booking_Setting__c];
for(AVTRRT__Placement__c plc1:plc)
{
if(plc1.AVTRRT_Next_Call_candidate__c!= null)
{
Task t1 = new Task();
t1.whatid=plc1.id;
t1.Task_Type__c='Candidate';
t1.OwnerId = plc1.AVTRRT__Recruiter__r.id;
t1.ActivityDate=plc1.AVTRRT_Next_Call_candidate__c;
t1.subject=csTaskValues.Candidate_Task_Subject__c;
t1.Description =csTaskValues.Candidate_Task_Comments__c;
n1.add(t1);
}
if(plc1.AVTRRT_Next_Call_Client__c!= null)
{
Task t2 = new Task();
t2.whatid=plc1.id;
t2.Task_Type__c='client';
t2.ActivityDate=plc1.AVTRRT_Next_Call_Client__c;
t2.OwnerId = plc1.AVTRRT__Assigned_Recruiter__r.id;
t2.subject=csTaskValues.Client_Task_Subject__c;
t2.Description =csTaskValues.Client_Task_Comments__c;
n2.add(t2);
}
insert n1;
insert n2;
}
update plc;
for(AVTRRT__Placement__c plc2:plc)
{
plc2.AVTRRT_Next_Call_Client__c=null;
plc2.AVTRRT_Next_Call_candidate__c=null;
update plc2;
}
PageReference customPage = new PageReference('/apex/TempBooking');
customPage.setRedirect(true);
return customPage;
}
This was so helpful for me..
Thank you for posting. :)
This was extremely helpful for me also... thanks for the post I've now solved my problem.
Jon
String[] res = xmlstring.split('/');
Integer acont = 0;
String cadena ='';
FacturasCM__c proy = new FacturasCM__c();
/*Se esta inicindo la lista cuando se recorre para la insercion y guardarla en la lista nos mando el siguiente error
INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call se suluciono de la siguiente forma*/
if(res.size() >1){
for (Integer a =1; a<res.size(); a++){
acont = (acont+1);
if(acont ==1){
/*Parametros CUC*/
proy.codigoEmpresa__c = id_c2;
/*Parametros PERIODO*/
proy.PeriodoFact__c = per;
/*Parametros CUENTA MAESTRA*/
proy.IdCm__c = res[a];
}
if(acont ==2){
/*Parametros MONTO*/
proy.Facturacion__c = res[a];
}
if(acont ==3){
/*Parametros CLIENTE*/
proy.Name__c = res[a];
}
if(acont ==4){
/*insert*/
system.debug('PRueba'+ proy);
acont = 0;
insert proy;
/*Colocando esto se suluciono limpiando la lista despues de la insercion*/
proy.clear();
}
}
}
return null;