• Grisha Pishchyk
  • NEWBIE
  • 5 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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