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
Manish Anand 10Manish Anand 10 

Trailhead Challenge:- Bulk Apex Triggers: Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.

Hi,
I was trying below trailhead challenge for Bulk Apex Triggers.

To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.
The Apex trigger must be called 'ClosedOpportunityTrigger'
With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
This challenge specifically tests 200 records in one operation.

I wrote below code:-
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
   List<Task> taskList = new List<Task>();
    for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new])
   {
    taskList.add(new Task(Subject = 'Follow Up Test Task',
    WhatId = opp.Id));
    }
   if(taskList.size()>0){
        insert taskList;
        }
    }
It throws an error-Variable Doesn't exist:ID at line 6 (WhatID=opp.ID)
What, I am missing here?

 
Mahesh DMahesh D
Hi Manish,

Please find the below code:
 
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
   List<Task> taskList = new List<Task>();
   //Iterate through the input records.
   for(Opportunity opp: Trigger.new) {
       // Check if the StageName is Closed Won and isChanged incase of update.
       if(opp.StageName == 'Closed Won' && (Trigger.isInsert || opp.StageName != Trigger.oldMap.get(opp.Id).StageName)) {
           taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
       }
   }
   
   // Check if the taskList is empty or not.
   if(!taskList.isEmpty()){
       insert taskList;
   }
}

Also added the comments as well so that you can able to understand the code easily.

FYI, I also tested the above code in my DE environment and it is working properly.

Regards,
Mahesh
JyothsnaJyothsna (Salesforce Developers) 
Hi Manish Anand,

Please check the below code that I have created and successfully passed in trailhead.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    List<Task> taskList = new List<Task>();   
    for(Opportunity opp : Trigger.new) {
        //Only create Follow Up Task only once when Opp StageName is to 'Closed Won' on Create
        if(Trigger.isInsert) {
            if(Opp.StageName == 'Closed Won') {
                taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
            }
        }
        //Only create Follow Up Task only once when Opp StageName changed to 'Closed Won' on Update
        if(Trigger.isUpdate) {
            if(Opp.StageName == 'Closed Won' 
            && Opp.StageName != Trigger.oldMap.get(opp.Id).StageName) {
                taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
            }
        }       
    }
    if(taskList.size()>0) {        
        insert taskList;        
    }    
}

Hope this helps you!
Best Regards,
Jyothsna
Muhammad AlaaMuhammad Alaa
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    //adds a 'Follow Up Test Task' to any opportunity set to 'Closed Won'
    List<Task> lstTask = new List<Task>();
    //Iterate over Opptys that are in this trigger and Closed Won
    for (Opportunity o : [SELECT Id, StageName FROM Opportunity WHERE Id IN :Trigger.New AND StageName = 'Closed Won']){
        //add Follow Up Task to this Oppty
        lstTask.add(new Task(Subject = 'Follow Up Test Task',
                            WhatId = o.Id));
    }
    
    if (lstTask.size() > 0)
        insert lstTask;
}

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Manish,
"Try this code."
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    List<Task> taskList = new List<Task>();
    
    for(opportunity opp : Trigger.new){
        if(opp.stagename == 'Closed Won'){
            taskList.add(new Task(Subject = 'Follow Up Test Task',
                                 WhatId=opp.Id));
        }
    } 
    if(taskList.size()>0){
        insert taskList;
    }
}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi