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
chithra srinivasanchithra srinivasan 

System.DmlException:Insert failed First exception on row 0; CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements

Hi,
     Need help in this Closed Opportunity -Trigger Bulk Apex trigger trail head challenge.The following error was received when trying to check the challenge eventhough the code worked and desired result was achieved. Could not check the challenge.
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements Trigger.ClosedOpportunityTrigger: line 19, column 1: []

The code is given below.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    try {
          List<Opportunity> Opps = [SELECT Id, StageName FROM Opportunity 
             WHERE StageName =:'Closed Won' AND Id IN :Trigger.New];
    
           List<Task> tasks = new List<Task>();
           Task t = new Task();

           for(Opportunity opp : Opps) {
               if(Trigger.isInsert || Trigger.isUpdate) {
                t.Subject = 'Follow Up Test Task';
                t.WhatId = opp.Id;
                tasks.add(t);
           
              }   
           }
           System.debug(tasks.size() + '  tasks size');
           if(tasks.size() > 0) {  
            upsert tasks;
           }    
       }catch (DmlException e) {
        System.debug('A DML exception has occurred: ' +
                    e.getMessage());
    }         
}
Thanks,
Chithra

 
Saurabh BSaurabh B
Hi Chithra, just initialize the object inside the for loop , I think you will not get the error. Like this,
 
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    try {
          List<Opportunity> Opps = [SELECT Id, StageName FROM Opportunity 
             WHERE StageName =:'Closed Won' AND Id IN :Trigger.New];
    
           List<Task> tasks = new List<Task>();
           

           for(Opportunity opp : Opps) {
               if(Trigger.isInsert || Trigger.isUpdate) {
                Task t = new Task();
                t.Subject = 'Follow Up Test Task';
                t.WhatId = opp.Id;
                tasks.add(t);
           
              }   
           }
           System.debug(tasks.size() + '  tasks size');
           if(tasks.size() > 0) {  
            upsert tasks;
           }    
       }catch (DmlException e) {
        System.debug('A DML exception has occurred: ' +
                    e.getMessage());
    }         
}

Please mark this as Best Answer if it helps you!
 
chithra srinivasanchithra srinivasan
Thanks Saurabh.That works. 
Saurabh BSaurabh B
Chithra,  please mark this as BEST ANSWER if it helps you!
Grisha PishchykGrisha Pishchyk
Saurabh B  I spent an hour on this, and if I had not found your post, I would have spent even more I think. Thx so much!

P.S.
Solution was here "Trigger.isInsert || Trigger.isUpdate"
Pratiksha Jadhav 24Pratiksha Jadhav 24
Thank-you 
Saurabh B and 
Grisha Pishchyk you both have solved my problem within seconds!!
But can you please provide its brief explanation!!