You need to sign in to do that
Don't have an account?
Trailhead: Bulk Apex Trigger
Unable to complete the challenge for the Apex Trigger using Bulk
error:Executing against the trigger does not work as expected.
Trigger code:
trigger ClosedOpportunityTrigger on Opportunity (before insert,before update)
{
List <task> taskinsert= new List<task>();
for(Opportunity o: Trigger.new)
{
if(o.StageName=='Closed Won')
{
Task t= new Task();
t.Subject='Follow up Task';
t.whatid=o.id;
taskinsert.add(t);
}
}
insert taskinsert;
}
error:Executing against the trigger does not work as expected.
Trigger code:
trigger ClosedOpportunityTrigger on Opportunity (before insert,before update)
{
List <task> taskinsert= new List<task>();
for(Opportunity o: Trigger.new)
{
if(o.StageName=='Closed Won')
{
Task t= new Task();
t.Subject='Follow up Task';
t.whatid=o.id;
taskinsert.add(t);
}
}
insert taskinsert;
}
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'.
Thanks,
Karanraj
http://www.karanrajs.com
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;
}
}
Jackson, I think your problem is at the beginning. You use before insert, before update, but you need to create the task after an Opportunity is created or updated. Otherwise you don't have an Id to connect to.
I have been having a similar problem and no matter what I do, I cannot fix it. Does anyone knows what I'm doing wrong? Any help is apreciatted:
Either you reflect that in your trigger or make the field not required.
I just added size check to his code as below: