I am trying to follow a single trigger approach.
- there are several resources and patterns for it. I am doing something very simple
My main question: Whenever we need to add new functionality to the trigger (aka a new service class), how do I reference it? which data records am I passing to it? I don't want, nor think, that all logic needs to go in a single handler and/or class. So, if I seperate the logic to several 'Service classes' what's the approach to reference the data set? Can I always use either:
- trigger.new
- trigger.newmap
are they static to the trigger?
Do I need to return form a service class the list of 'trigger.new' that it received and may be modified?
Trigger:
trigger OpportunityTrigger on IJS__c (
before insert, after insert,
before update, after update,
before delete, after delete)
{
OpportunityHandler handler = new OpportunityHandler();
if (Trigger.isBefore)
{
if (Trigger.isInsert)
{
handler.OpportunityService1(trigger.new);
handler.OpportunityService2(trigger.new);
}
if (Trigger.isUpdate) { }
if (Trigger.isDelete) { }
}
if (Trigger.IsAfter){
if (Trigger.isInsert){ }
if (Trigger.isUpdate){ }
if (Trigger.isDelete){ }
}
}
A Single Trigger Handler:
public without sharing class OpportunityHandler
{
public void OpportunityService1 (List<Opportunity> objList)
{
// call Servicefunction1 and modify the data
}
public void OpportunityService2 (List<Opportunity> objList)
{
// call ServiceFunction2 and modify the data
}
}
Several Service Classes
public without sharing class OpportunityService1 {
public OpportunityService1() {}
public void ServiceFunction1(){
for(Opportunity opp :updatedOpportunitiesList)
{
opp.recordtypeid = system.label.SomeRecordTypeId;
}
}}
Second Service Class:
public without sharing class OpportunityService2 {
public OpportunityService2() {}
public void ServiceFunction1(){
for(Opportunity opp :updatedOpportunitiesList)
{
opp.Name = 'set a new name';
}
}}
}