+ Start a Discussion
Pierre-AlainPierre-Alain 

Error "Executing against the trigger does not work as expected."

Hi - when doing the challenge "Bulk Apex Trigger" in Trailhead I get the error message :"Executing against the trigger does not work as expected."

I have checked the name of the class, task name as mentioned in the challenge description.

I have copied the code below :

trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
  List<Task> taskList = new List<Task>();

    //If an opportunity is inserted or updated with a stage of 'Closed Won'
    // add a task created with the subject 'Follow Up Test Task'.
    for (Opportunity opp : [SELECT Id,Name FROM Opportunity
                     WHERE Id IN :Trigger.new AND StageName = 'Closed Won']) {
       //add a task with subject 'Follow Up Test Task'.
       taskList.add(new Task(Subject='Follow Up Test Task', WhatId = opp.id ));                
  }
                          
    if (taskList.size() > 0) {
        insert taskList;
    }



Thank you
Pierre-Alain


 
Best Answer chosen by Pierre-Alain
Ravikant kediaRavikant kedia
First of all you dont need to query the records. In triggers you get those records in by Trigger.new. and if you choose your way which is wrong ( because of an extra sql query ) you need to add "Id IN Trigger.newmap.keyset()" instead of "Id IN :Trigger.new" because "Trigger.new" will return a list of Opportunity records for which it is fiered while "Trigger.newmap.keyset()" will return a set if ID.

Please select this as a best answer.

All Answers

Ravikant kediaRavikant kedia
Use below code to complete your requirement.

trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) 
{
    List<Task> taskList = new List<Task>();
    
    //If an opportunity is inserted or updated with a stage of 'Closed Won'
    // add a task created with the subject 'Follow Up Test Task'.
    for (Opportunity opp : Trigger.new) 
    {
       //add a task with subject 'Follow Up Test Task'.
       if(opp.StageName == 'Closed Won')
           taskList.add(new Task(Subject='Follow Up Test Task', WhatId = opp.id ));                
    }
                          
    if (taskList.size() > 0) 
    {
        insert taskList;
    }


if my answer helps resolve your query, please select it as a 'Best Answer' so that it benefits others and helps us improve the overall quality of the forums.
 
Pierre-AlainPierre-Alain
Thanks Ravikant,

After copying/pasting your code, it worked. By comparing boths codes I can see that the difference is ( my code in italic, your code in Bold )

for (Opportunity opp : [SELECT Id,Name FROM Opportunity
                     WHERE Id IN :Trigger.new AND StageName = 'Closed Won']) {

    

for (Opportunity opp : Trigger.new) 
    {
       if(opp.StageName == 'Closed Won')

           
Is there anything wrong about the code I added ( Italic ) ? I don't understand why the challenge would fail.

Thank you
Pierre-Alain
 
Ravikant kediaRavikant kedia
First of all you dont need to query the records. In triggers you get those records in by Trigger.new. and if you choose your way which is wrong ( because of an extra sql query ) you need to add "Id IN Trigger.newmap.keyset()" instead of "Id IN :Trigger.new" because "Trigger.new" will return a list of Opportunity records for which it is fiered while "Trigger.newmap.keyset()" will return a set if ID.

Please select this as a best answer.
This was selected as the best answer
Pierre-AlainPierre-Alain
ok thank for the clarifiications.
Pierre HausheerPierre Hausheer
Your code was good in the first place (the suggested if statement here is not optimised and your bulk SOQL is what is expected here) exept for the before trigger. Indeed, you should use after trigger, otherwise you won't have any value for the opportunities Id.

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
...

---
This is one year question old but it's showing on top of google search.
Vipul Dalal 9Vipul Dalal 9
You were not checking for old StageName if the trigger is updated.  Addting this code below as I still see this on top of google search.  Here's code that worked:

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    
    List<Task> OpTasklist = new List<Task>();
    
    // Iterate over opportunities that are in this trigger and have a stage of "Closed Won"
    for (Opportunity op: [SELECT id FROM Opportunity 
                          WHERE Id IN :Trigger.New AND
                          StageName =: 'Closed Won']) {
                              
                              if (((Trigger.IsUpdate) && (Trigger.oldMap.get(op.Id).StageName != 'Closed Won')) || 
                                  (Trigger.IsInsert)) {
                                      OpTaskList.add(new Task (Subject='Follow Up Test Task', 
                                                               WhatId = op.Id)); }          
                          }
    
    If (OpTaskList.size() > 0) { 
        Insert OpTaskList ;
    }   
}
Martin raj 4Martin raj 4
Thank you so much for the information. Very good post

https://apkmoto.com/

yowhatsapp apk (https://apkmoto.com/yowhatsapp-apk/)