You need to sign in to do that
Don't have an account?

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
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
Please select this as a best answer.
All Answers
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.
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
Please select this as a best answer.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
...
---
This is one year question old but it's showing on top of google search.
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 ;
}
}
https://apkmoto.com/
yowhatsapp apk (https://apkmoto.com/yowhatsapp-apk/)
Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well.
I wanted to thank you for this websites! Thanks for sharing.
https://pubg-apk.com
Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well.
I wanted to thank you for this websites! Thanks for sharing. Great websites!
https://apkfasak.com/
kingroot for pc
This app will help you to get access to the root of your device.