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
Jayson Faderanga 14Jayson Faderanga 14 

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;


}
Mohit Bansal6Mohit Bansal6
Jayson

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.
Amit  TrivediAmit Trivedi
Hello,

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
Ajay K DubediAjay K Dubedi
Your trigger is not working properly because you are iterating the for loop on trigger.new. Instead of this, try using the below code.
 
trigger oppCreate on Account (after insert) {
List<Opportunity> newlyCreatedOpp =  new List<Opportunity>();
List<Account> testList = [SELECT Id, Name,numberofEmployees, (SELECT Id, Name, CloseDate, AccountId, StageName from opportunities) from account where Id=: trigger.new[0].Id];

for (Account newAcc : testList)
{
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;
}

Let me know if this helps!
Jayson Faderanga 14Jayson Faderanga 14
Ajay you nailed it!! thanks for all your help! I realized my mistake! keep on rockin guys!