I'd have to agree with @Eric that the first option is better since it is reusable in other places, besides triggers.
So say, you want to do the same processing in a controller or whatever, you can reuse it there with no problems.
I personally tend to do a mix of the two.
public with sharing class HandlerExample
{
public static void HandleInTrigger(Map<Id, Object> oldMap, List<sObject> newList)
{
//Filter out the records that you want using some method, like Filter...
List<sObject> listToProcess = Filter(oldMap, newList);
ProcessRecords(listToProcess);
}
public static void ProcessRecords(List<sObject> listToProcess)
{
//Execute Logic here...
}
}
//In your trigger
HandlerExample.HandleInTrigger(Trigger.oldMap, Trigger.New);
//In a controller
HandlerExample.ProcessRecords(yourListToProcess);
I have some trigger specific logic simply to filter and pass it to the processing method. I can still use the ProcessRecords
method anywhere else without issue if need be.
There is a slight problem (or big problem to me) with the example at 18 minutes. Using the ALEADY_RAN
variable to true is great to protect against trigger recursion. However, if you use the dataloader on say, 400 records, how many records with the processAfterUpdate
touch?
It will only touch 200 records... This is because the static variables are reset per transaction and updating the data in bulk, although it is doing it this 200 records at a time, it is still considered a SINGLE transaction. So you will need to toggle that flag accordingly for those scenarios. Figured I'd throw that out there since this bit me big time in my last project.