Apex triggers are used to manage records in Salesforce. It enables us to perform operations before or after the event is completed. In the “before event” we generally perform validations to prevent wrong entry into the database. In the “after event” we perform operations such as creating or updating a related record. The trigger is a very powerful mechanism using which we can do operations such as executing SOQL, executing DML operations, calling apex class methods.
public interface TriggerHandler
{
void beforeInsert(List<SObject>
newRecordsList);
void beforeUpdate(List<SObject>
oldRecordsList, List<SObject> newRecordsList, Map<ID, SObject>
oldRecordsMap, Map<ID, SObject> newRecordsMap);
void beforeDelete(List<SObject>
oldRecordsList, Map<ID, SObject> oldRecordsMap);
void afterDelete(List<SObject>
oldRecordsList, Map<ID, SObject> oldRecordsMap);
void afterInsert(List<SObject>
newRecordsList, Map<ID, SObject> newRecordsMap);
void afterUpdate(List<SObject>
oldRecordsList, List<SObject> newRecordsList, Map<ID, SObject>
oldRecordsMap, Map<ID, SObject> newRecordsMap);
void afterUndelete(List<SObject>
newRecordsList, Map<ID, SObject> newRecordsMap);
}
public without sharing
class OpportunityTriggerHandler implements TriggerHandler {
public void
beforeInsert(List<Opportunity> newOppList) {
helperClass.methodToHandleBeforeInsert();
}
public void
beforeUpdate(List<Opportunity> oldOppList, List<Opportunity>
newOppList, Map<ID, SObject> oldOppMap, Map<ID, SObject> newOppMap)
{
helperClass.methodToHandleBeforeUpdate();
}
public void
beforeDelete(List<Opportunity> oldOppList, Map<ID, SObject>
oldOppMap) {
helperClass.methodToHandleBeforeDelete();
}
public void
afterDelete(List<Opportunity> oldOppList, Map<ID, SObject>
oldOppMap) {
helperClass.methodToHandleAfterDelete();
}
public void
afterInsert(List<Opportunity> newOppList, Map<ID, SObject>
newOppMap) {
helperClass.methodToHandleAfterInsert();
}
public void
afterUpdate(List<Opportunity> oldOppList, List<Opportunity>
newOppList, Map<ID, SObject> oldOppMap, Map<ID, SObject> newOppMap)
{
helperClass.methodToHandleAfterUpdate();
}
public void
afterUndelete(List<Opportunity> newOppList, Map<ID, SObject>
newOppMap) {
helperClass.methodToHandleAfterUndelete();
}
}
public without sharing
class OpportunityTriggerHelper {
public void methodToHandleBeforeInsert() {
System.debug('Inside
methodToHandleBeforeInsert');
}
public void methodToHandleBeforeUpdate() {
System.debug('Inside
methodToHandleBeforeUpdate');
}
public void methodToHandleBeforeDelete() {
System.debug('Inside methodToHandleBeforeDelete');
}
public void methodToHandleAfterDelete() {
System.debug('Inside
methodToHandleAfterDelete');
}
public void methodToHandleAfterInsert() {
System.debug('Inside
methodToHandleAfterInsert');
}
public void methodToHandleAfterUpdate() {
System.debug('Inside
methodToHandleAfterUpdate');
}
public void methodToHandleAfterUndelete() {
System.debug('Inside
methodToHandleAfterUndelete');
}
}
trigger OpportunityTrigger
on Opportunity(before insert, before update, before delete, after delete, after
insert, after update, after undelete) {
OpportunityTriggerHandler handler =
new OpportunityTriggerHandler();
switch on Trigger.operationType {
when BEFORE_INSERT {
handler.beforeInsert(Trigger.new);
}
when BEFORE_UPDATE {
handler.beforeUpdate(Trigger.old, Trigger.new, Trigger.oldMap,
Trigger.newMap);
}
when BEFORE_DELETE {
handler.beforeDelete(Trigger.old, Trigger.oldMap);
}
when AFTER_DELETE {
handler.afterDelete(Trigger.old, Trigger.oldMap);
}
when AFTER_INSERT {
handler.afterInsert(Trigger.new, Trigger.newMap);
}
when AFTER_UPDATE {
handler.afterUpdate(Trigger.old, Trigger.new, Trigger.oldMap,
Trigger.newMap);
}
when AFTER_UNDELETE {
handler.afterUndelete(Trigger.new, Trigger.newMap);
}
}
}
No comments:
Post a Comment