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
Ben MyhreBen Myhre 

What is wrong with my code on Developer Beginner > Apex Triggers > Bulk Apex Triggers

This is the challenge:

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.

Here is my code:
trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
    List<Opportunity> opportunities = [SELECT Id, StageName
                                       FROM Opportunity
                                       WHERE Id
                                       IN :Trigger.New];
	List<Task> tasksToUpdate = new List<Task>();
    System.debug('##### OPS' + opportunities);
    for(Opportunity o : opportunities){
        System.debug('##### ' + o.StageName);
        if(o.StageName == 'Closed Won'){
            Task thisTask = new Task(WhatId = o.Id, Subject = 'Follow Up Test Task');
            tasksToUpdate.add(thisTask);
            System.debug('##### ' + tasksToUpdate);
        }
    }
    insert tasksToUpdate;
}

When I try to validate through trailhead, it gives a "Challenge Not yet complete... here's what's wrong: 
Executing against the trigger does not work as expected." error.

I added some debug print and it seems to show that the soql statement just does not pull any results, so it does not enter the if statement. It seems a pretty straightforward soql statement to me, but I must be missing something. This happens no matter if I add or update an item.

Thanks in advance
 
Best Answer chosen by Ben Myhre
Panduranga GollaPanduranga Golla
please try this code--

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
   
    List<Task> tasksToUpdate = new List<Task>();
   // System.debug('##### OPS' + opportunities);
    for(Opportunity o : trigger.new){
        System.debug('##### ' + o.StageName);
        if(o.StageName == 'Closed Won'){
            Task thisTask = new Task(WhatId = o.Id, Subject = 'Follow Up Test Task');
            tasksToUpdate.add(thisTask);
            System.debug('##### ' + tasksToUpdate);
        }
    }
    insert tasksToUpdate;
}

All Answers

Panduranga GollaPanduranga Golla
please try this code--

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
   
    List<Task> tasksToUpdate = new List<Task>();
   // System.debug('##### OPS' + opportunities);
    for(Opportunity o : trigger.new){
        System.debug('##### ' + o.StageName);
        if(o.StageName == 'Closed Won'){
            Task thisTask = new Task(WhatId = o.Id, Subject = 'Follow Up Test Task');
            tasksToUpdate.add(thisTask);
            System.debug('##### ' + tasksToUpdate);
        }
    }
    insert tasksToUpdate;
}
This was selected as the best answer
Ben MyhreBen Myhre
AFTER insert and AFTER update... thank you.