-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBulk Apex Triggers
36 lines (28 loc) · 1.44 KB
/
Bulk Apex Triggers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Challenge 2:
Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.
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.
Solution:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
List<Task> taskList = new List<Task>();
//first way
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));
}
//second way and we should use this
/*
for(opportunity opp: Trigger.New){
if(opp.StageName!=trigger.oldMap.get(opp.id).stageName)
{
taskList.add(new Task(Subject = 'Follow Up Test Task',
WhatId = opp.Id));
}
}
*/
if(taskList.size()>0){
insert tasklist;
}
}