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
EtienneCoutantEtienneCoutant 

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; }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

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

EtienneCoutantEtienneCoutant

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... 

 

wesnoltewesnolte

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

 

 

This was selected as the best answer
EtienneCoutantEtienneCoutant
Thanks so much for pointing out that really dumb mistake!!
streetstreet

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;
}

success22success22

This was so helpful for me..

 

Thank you for posting. :)

JonathanMClarkeJonathanMClarke

This was extremely helpful for me also... thanks for the post I've now solved my problem.

Jon

neeedhelpneeedhelp
Thanks for this wonderful Post...Added +1 for your Answer
john Saavedrajohn Saavedra
List<FacturasCM__c> Existente = [SELECT codigoEmpresa__c, PeriodoFact__c, IdCm__c, Facturacion__c, Name__c FROM FacturasCM__c ];
            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;
           
Santosh kyatanavarSantosh kyatanavar
Same list your inserting secound time or twice we will face this error, we need make sure insert one list at time.
Mamathesh M RMamathesh M R
This error generally raise when you try to insert data which is just inserted and Id generated for this data, This exception behave like the data is inserted but it will through you an error, So to avoid this use Upsert insted of Insert or have a look at any custom controler / vfpage is communicating with this class, if in case then wait for second dont click twice save button.