I've been researching some best practices and trigger frameworks, and I found Kevin O'Haras TriggerHandler Framework. Now I don't fully understand it, I don't understand how to reference the Trigger handler to my actual trigger, I know I'm suppose to put extend
but how does everything else "handle" my trigger...? The following is a simple trigger to auto populate a field, so for future reference and knowledge how should I have gone about building this trigger using Kevin's framework?
Questions from Kevins Git -- Not sure what these are saying..
In your trigger handler, to add logic to any of the trigger contexts, you only need to override them in your trigger handler.
To use the trigger handler, you only need to construct an instance of your trigger handler within the trigger handler itself and call the run() method.
I'm not asking anyone to actually implement it, I'm just trying to better understand how to do it, and I think by using an example I wrote would help me.
trigger updateSamplingTransZone on Sampling__c(before insert, before update)
{
Set<String> validCSet = new set<String>();
if(Trigger.IsInsert || Trigger.isUpdate)
{
for(Sampling__c s: Trigger.new)
{
if (s.Country__c != null || s.Country__c != '')
{
validCSet.add(s.Country__c);
system.debug('This is ValidCSet ' + validCSet + '\n');
}
}
List<Validation_Region__c> validationRegionList = [Select Id, Transportation_Zone__c, Country_Key__c from Validation_Region__c where Country_Key__c IN: validCSet];
Map<String, Validation_Region__c> validationRegionMap = new Map <String, Validation_Region__c>();
for (Validation_Region__c vr: validationRegionList)
{
validationRegionMap.put(vr.Country_Key__c, vr);
}
system.debug('This is validationRegionMap: ' + validationRegionMap);
for (Sampling__c s: Trigger.new)
{
if(s.SAP_Transportation_Zone__c != null || s.SAP_Transportation_Zone__c != '')
{
s.SAP_Transportation_Zone__c = null;
}
if (s.Override__c == 'Yes')
{
try{
s.SAP_Transportation_Zone__c = validationRegionMap.get(s.Country__c).Transportation_Zone__c;
}catch(System.NullPointerException e){
system.debug('Override SAP Address with Drop-Ship Info has been flagged "Yes". The country needs to be populated in this case.');
}
}
}
}
}