You need to sign in to do that
Don't have an account?
Manish Anand 10
Trailhead Challenge:- Bulk Apex Triggers: Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.
Hi,
I was trying below trailhead challenge for Bulk Apex Triggers.
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.
I wrote below code:-
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
List<Task> taskList = new List<Task>();
for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new])
{
taskList.add(new Task(Subject = 'Follow Up Test Task',
WhatId = opp.Id));
}
if(taskList.size()>0){
insert taskList;
}
}
It throws an error-Variable Doesn't exist:ID at line 6 (WhatID=opp.ID)
What, I am missing here?
I was trying below trailhead challenge for Bulk Apex Triggers.
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.
I wrote below code:-
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
List<Task> taskList = new List<Task>();
for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new])
{
taskList.add(new Task(Subject = 'Follow Up Test Task',
WhatId = opp.Id));
}
if(taskList.size()>0){
insert taskList;
}
}
It throws an error-Variable Doesn't exist:ID at line 6 (WhatID=opp.ID)
What, I am missing here?
Please find the below code:
Also added the comments as well so that you can able to understand the code easily.
FYI, I also tested the above code in my DE environment and it is working properly.
Regards,
Mahesh
Please check the below code that I have created and successfully passed in trailhead.
Hope this helps you!
Best Regards,
Jyothsna
"Try this code." If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi