You need to sign in to do that
Don't have an account?
Pierre-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
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.
The Code Is Right
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 ;
}
}
Thanks
Regards
Geometry Dash APK (https://geometrydashapk.co/)
==================Apex Class =======================
public class OpportunityApexClass {
public static void opportunityTrigger(list<Opportunity> varOpportunity)
{
list<Task> varTask = new list<Task>();
for(Opportunity varopp : varOpportunity)
{
if(varopp.StageName == 'Closed Won')
{
Task varTa = New Task();
varTa.Subject ='Follow Up Test Task';
varTa.Status ='Not Started';
varTa.WhatId = varopp.Id;
varTask.add(varTa);
}
}
insert varTask;
}
}
===================Trigger==========================
trigger ClosedOpportunityTrigger on Opportunity (before insert,before update) {
if(Trigger.isbefore == True && Trigger.isinsert == True)
{
OpportunityApexClass.opportunityTrigger(Trigger.new);
}
if(Trigger.isbefore == True && Trigger.isupdate == True)
{
OpportunityApexClass.opportunityTrigger(Trigger.new);
}
}
due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked
to see new information on your blog. Text Fonts Generator (https://textfontsgenerator.blogspot.com/)
Apex Launcher (https://apexlauncherapk.com/)
Launcher Apk (https://launcherapk.com/)
https://www.apksforfree.com/
https://www.apksforfree.com/oreo-tv-apk/
so good blod, thanks
tap here (https://swissnativecasinos.com/online-casino/zimpler)
Thank you for sharing such a nice post on your blog keep it up and share more. home network security solution (https://dmoat.com/)
https://www.apksall.com/
https://www.apksall.com/toon-app-latest-apk/