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
nimdharnimdhar 

INVALID_FIELD_FOR_INSERT_UPDATE Trigger error

Hi,

I am getting the following error when trying to insert an object called IncentiveCount. I have a trigger which runs when inserting DFR object. DFR object has link to an Account. When inserting DFR object I should see the Account Id and get all the Incentives associated with that Account. After that I should see if there is any incentive which has start date equal to DFR date. If it has I need to insert a record in Incentive Count that has a DFR Id, Incentive Id, Account Id. When I did this with the below code
for(integer i=0;i<accountIds.size();i++)
 {
if(entries.containsKey(accountIds[i]) && (!uniqueAccountIdsToLoop.contains(accountIds[i])))
{
uniqueAccountIdsToLoop.add(accountIds[i]);
List<Incentive__c> incentivesToLoop= new List<Incentive__c>();
incentivesToLoop = entries.get(accountIds[i]);
for(integer j=0;j<incentivesToLoop.size();j++)
{
for(Doctors_First_Report__c newDFRs: Trigger.new.deepClone())
{

if(newDFRs.Account__c<>null && newDFRs.Account__c==accountIds[i])
{

if(incentivesToLoop[j].Start_Date__c<>null && incentivesToLoop[j].End_Date__c<>null)
{
if(newDFRs.DFR_Date__c >= incentivesToLoop[j].Start_Date__c && newDFRs.DFR_Date__c <= incentivesToLoop[j].End_Date__c)
{
IncentiveCount__c ic=new IncentiveCount__c();
ic.Account__c=accountIds[i];
ic.Doctor_s_First_Report__c=newDFRs.Id;
ic.Incentive__c=incentivesToLoop[j].Id;
incentivesToInsert.add(ic);
}
}
}
}
}
insert incentivesToInsert;
}
}
}
I get the below exception
caused by: System.DmlException: Insert failed. First exception on row 0 with id a0DR0000000ktPAMAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Can anyone please let me know what this error is about?

Thanks,
Nimdhar

dmchengdmcheng
What does "entries" contain?  Account IDs and the incentive records associated with each acct?

Also - I don't understand why you have DFRs in an inner "for" loop.  Since the trigger is on DFR, shouldn't the DFR for loop be the outermost loop of your code?


Message Edited by dmcheng on 12-28-2008 06:05 PM

Message Edited by dmcheng on 12-28-2008 06:05 PM