You need to sign in to do that
Don't have an account?
Gtennent
Bulk Apex Triggers Help
I'm trying to complete the Bulk Apex Triggers challenge. I understand what needs to be done, but I don't know how to do it. Also, I'm getting the very helpful error "Executing against the trigger does not work as expected."
The directions are:
The directions are:
- 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.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) { List<Task> newTasks = new List<Task>(); for(Opportunity opp : [SELECT Id, Name, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id in :Trigger.new]){ newTasks.add(new Task(Subject = 'Follow Up Test Task.', WhatId = opp.Id)); } If(newTasks.size() > 0){ insert newTasks; } }Where have I gone wrong, why is it wrong, and how can I fix it? Please don't just give me code that works that doesn't helpe me learn. I want to understand why the code works, and how to get there from where I am. Thanks!
You are not checking that the inserting opportunity contains StageName = 'Closed Won'. That may be the reason you are not able to complete the challenge.
Refer this code:
Thanks,
Dhanya