You need to sign in to do that
Don't have an account?
getting error on Trigger
I am getting an error when I tried to insert more than 200 account records using data loader. I created the below trigger code and the error message is shown below. any idea guys?
oppCreate: execution of AfterInsert
caused by: System.DmlException: Insert failed. First exception on row 0 with id 00628000002gPTkAAM; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Trigger.oppCreate: line 19, column 1
trigger oppCreate on Account (after insert) {
List<Opportunity> newlyCreatedOpp = new List<Opportunity>();
for (Account newAcc : Trigger.new)
{
Opportunity createOpp = new Opportunity ();
createOpp.Name = newAcc.name + 'Opportunity';
createOpp.CloseDate = system.today().addDays(30);
createOpp.AccountId = newAcc.Id;
if (newAcc.numberofEmployees> 100) {
createOpp.StageName = 'Prospecting'; }
else if(newAcc.numberofEmployees< 100) {
createOpp.StageName = 'Qualification'; }
else {}
newlyCreatedOpp.add(createOpp);
}
insert newlyCreatedOpp;
}
oppCreate: execution of AfterInsert
caused by: System.DmlException: Insert failed. First exception on row 0 with id 00628000002gPTkAAM; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Trigger.oppCreate: line 19, column 1
trigger oppCreate on Account (after insert) {
List<Opportunity> newlyCreatedOpp = new List<Opportunity>();
for (Account newAcc : Trigger.new)
{
Opportunity createOpp = new Opportunity ();
createOpp.Name = newAcc.name + 'Opportunity';
createOpp.CloseDate = system.today().addDays(30);
createOpp.AccountId = newAcc.Id;
if (newAcc.numberofEmployees> 100) {
createOpp.StageName = 'Prospecting'; }
else if(newAcc.numberofEmployees< 100) {
createOpp.StageName = 'Qualification'; }
else {}
newlyCreatedOpp.add(createOpp);
}
insert newlyCreatedOpp;
}
I think your trigger is going in recursion, which is making this error to appear.
Regards
Mohit Bansal
In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
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.
Thanks,
Amit Trivedi
Let me know if this helps!