Skip to main content
Esti Nevo (Yes) a posé une question dans #Trailhead Challenges

I create this trigger:    trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {   List<Task> taskList = new List<Task>();     List<Opportunity> oppList = [SELECT StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.New];     for(Opportunity opp: oppList){   taskList.add(new Task(Subject = 'Follow Up Test Task', Whatid = opp.ID));   }     if(taskList.size()>0){   insert taskList;   }     }       and the error is:     Challenge not yet complete in My Trailhead Playground 4   We created an opportunity and expected it to have an associated task, but it didn’t. Make sure your Apex trigger inserts the task into the database     i dont understand why. 

1 réponse
  1. 28 mars 2022, 16:03

    You oppList that your opp variable is based on does not contain the OpportunityId.  So your whatId is being set to null.  You need to make sure your query is returning all opportunity fields that you will then need for assigning values.  You currently are returning a list of opportunity stages only.

0/9000