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
Jina ChetiaJina Chetia 

Error in trigger INVALID_FIELD_FOR_INSERT_UPDATE

Hi,

 

I have trigger on a custom object CPP__c on update where I am creating history records for that object if any desired value changes in the updated record. I am not using nay new id while inserting but still I get this error

 

Update_CPP_change_history: execution of AfterUpdate

caused by: System.DmlException: Insert failed. First exception on row 0 with id a04R000000141WOIAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

 

The trigger works fine on a single update, it only fails when I try to do a mass update through Apex Data Loader by matching the lookup fields on the CPP__c object.

 

Can someone please help me in solving this issue? I have inserting the code snippet below

 

trigger Update_CPP_change_history on CPP__c (before delete, after insert, after update) { List<CPP_Change_History__c> CPPs = new List<CPP_Change_History__c>{}; if (Trigger.isInsert) { for (CPP__c a : Trigger.new) { CPP_Change_History__c ch = new CPP_Change_History__c( Account_Name__c = a.Account__c, CPP__c = a.id, Business_Lost_Reason_New__c = a.Business_Lost_Reason__c, Business_Under_Threat_Reason_New__c = a.Business_Under_Threat_Reason__c, Business_Won_Reason_New__c = a.Business_Won_Reason__c, Change_Date__c = System.today(), Price_New__c = a.per_UOM__c, Status_New__c = a.Current_Status__c, Supplier_New__c = a.Supplier_Name__c, Producer_New__c = a.Producer_Name__c, Volume_New__c = a.Annual_KGs__c ); CPPs.add(ch); } insert CPPs; } else if (Trigger.isUpdate) { for (Integer i = 0; i < Trigger.new.size(); i++) { if ((Trigger.old[i].Supplier_Name__c != Trigger.new[i].Supplier_Name__c)|| (Trigger.old[i].Producer_Name__c != Trigger.new[i].Producer_Name__c) || (Trigger.old[i].Business_Lost_Reason__c != Trigger.new[i].Business_Lost_Reason__c) || (Trigger.old[i].Business_Under_Threat_Reason__c != Trigger.new[i].Business_Under_Threat_Reason__c)|| (Trigger.old[i].Business_Won_Reason__c != Trigger.new[i].Business_Won_Reason__c) || (Trigger.old[i].per_UOM__c != Trigger.new[i].per_UOM__c) || (Trigger.old[i].Current_Status__c != Trigger.new[i].Current_Status__c)|| (Trigger.old[i].Annual_KGs__c != Trigger.new[i].Annual_KGs__c) ) { //Creat CPP History only if certain fields change CPP_Change_History__c ch = new CPP_Change_History__c( CPP__c = Trigger.new[i].id, Change_Date__c = System.today(), Account_Name__c = Trigger.new[i].Account__c); if(Trigger.old[i].Supplier_Name__c != Trigger.new[i].Supplier_Name__c){ ch.Supplier_New__c = Trigger.new[i].Supplier_Name__c; ch.Supplier_Old__c = Trigger.old[i].Supplier_Name__c; } if(Trigger.old[i].Producer_Name__c != Trigger.new[i].Producer_Name__c){ ch.Producer_New__c = Trigger.new[i].Producer_Name__c; ch.Producer_Old__c = Trigger.old[i].Producer_Name__c; } if(Trigger.old[i].Business_Lost_Reason__c != Trigger.new[i].Business_Lost_Reason__c){ ch.Business_Lost_Reason_New__c = Trigger.new[i].Business_Lost_Reason__c; ch.Business_Lost_Reason_Old__c = Trigger.old[i].Business_Lost_Reason__c; } if(Trigger.old[i].Business_Under_Threat_Reason__c != Trigger.new[i].Business_Under_Threat_Reason__c){ ch.Business_Under_Threat_Reason_New__c = Trigger.new[i].Business_Under_Threat_Reason__c; ch.Business_Under_Threat_Reason_Old__c = Trigger.old[i].Business_Under_Threat_Reason__c; } if(Trigger.old[i].Business_Won_Reason__c != Trigger.new[i].Business_Won_Reason__c){ ch.Business_Won_Reason_New__c = Trigger.new[i].Business_Won_Reason__c; ch.Business_Won_Reason_Old__c = Trigger.old[i].Business_Won_Reason__c; } if(Trigger.old[i].per_UOM__c != Trigger.new[i].per_UOM__c){ ch.Price_New__c = Trigger.new[i].per_UOM__c; ch.Price_Old__c = Trigger.old[i].per_UOM__c; } if(Trigger.old[i].Current_Status__c != Trigger.new[i].Current_Status__c){ ch.Status_New__c = Trigger.new[i].Current_Status__c; ch.Status_Old__c = Trigger.old[i].Current_Status__c; } if(Trigger.old[i].Annual_KGs__c != Trigger.new[i].Annual_KGs__c){ ch.Volume_New__c = Trigger.new[i].Annual_KGs__c; ch.Volume_Old__c = Trigger.old[i].Annual_KGs__c; } CPPs.add(ch); } insert CPPs; } } else if (Trigger.isDelete){ for (Integer i = 0; i < Trigger.old.size(); i++) { CPP_Change_History__c[] doomedCPP = [select Id,Name from CPP_Change_History__c where CPP__C = : Trigger.old[i].id]; Database.DeleteResult[] DR_Dels = Database.delete(doomedCPP); } } }

 

Thanks,

Jina

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
because in the isUpdate case you're calling insert CCPs inside the for loop, you're trying to insert rows you've already inserted, you need to move it outside the for loop.

All Answers

SuperfellSuperfell
because in the isUpdate case you're calling insert CCPs inside the for loop, you're trying to insert rows you've already inserted, you need to move it outside the for loop.
This was selected as the best answer
Jina ChetiaJina Chetia
Thanks for pointing that out, never caught my eye :smileyhappy: