Skip to content

Apex Architecture

Morgan Marchese edited this page Feb 22, 2021 · 6 revisions

Naming Conventions

Triggers

  • Trigger Names should be the first 33 characters of the Object API Name (excluding the __c for custom objects), followed by the word 'Trigger'.
trigger AccountTrigger on Account(){}

Object Handler Classes

  • Object Handler class names should be the first 32 characters of the Object API Name (excluding the __c for custom objects), followed by the word 'TrigHdlr'.
public with sharing class AccountTriggerHandler extends TriggerHandlerExtension implements TriggerHandlerInterface{}

Batch Classes

  • Batch Class names should accurately reflect what the batch job is for, and should end with '_Batch'
global class AccountAlignmentNotification_Batch implements Database.Batchable<sObject>{}

Scheduled Classes

  • Scheduled Class names should accurately reflect what the scheduled job is for, and should end with '_Sched'
global class AccountAlignmentNotification_Sched implements Schedulable{}

Future Methods

  • Future method names should end with the words 'InFuture'
@future
public static void updateAccountStatusInFuture(){}

Triggers

  1. Every object should only have one trigger.
  2. Triggers should contain no business logic.
  3. Triggers should execute in all contexts.
  4. Triggers should call one Object Handler class using the DispatchTriggerHandler.dispatchHandlerToFire() method.
trigger AccountTrigger on Account (
  before insert, 
  before update, 
  before delete, 
  after insert, 
  after update, 
  after delete, 
  after undelete
) {
    DispatchTriggerHandler.dispatchHandlerToFire(
      new AccountTriggerHandler(), 
      Bypass_Triggers__mdt.Account_Bypass__c
    );
}

Object Classes

  1. Each Object should have one Object Handler Class.
  2. That Object handler class should extend TriggerHandlerExtension and implement TriggerHandlerInterface
  3. Object Handler classes should have public methods that match all trigger contexts.
public with sharing class AccountTriggerHandler extends TriggerHandlerExtension implements TriggerHandlerInterface  
{
  //Field used for Account segmentation logic
  private Schema.sObjectField marketField = Account.ZTS_EU_Market__c.getDescribe().getSObjectField();

  public void beforeInsert(
    List<SObject> newList
  ){
    // Do something
  }
    
  public void beforeUpdate(
    List<SObject> oldList, 
    Map<Id, SObject> oldMap, 
    List<SObject> newList, 
    Map<Id, SObject> newMap
  ){ 
    // Do something
  }
    
  public void beforeDelete(
    List<SObject> oldList, 
    Map<Id, SObject> oldMap
  ){
    // Do something
  }
    
  public void afterInsert(
    List<SObject> newList, 
    Map<Id, SObject> newMap
  ){
    // Do something
  }
    
  public void afterUpdate(
    List<SObject> oldList, 
    Map<Id, SObject> oldMap, 
    List<SObject> newList, 
    Map<Id, SObject> newMap
  ){
    // Do something
  }
    
  public void afterDelete(
    List<SObject> oldList, 
    Map<Id, SObject> oldMap
  ){
    // Do something
  }

  public void afterUndelete(
    List<SObject> newList, 
    Map<Id, SObject> newMap
  ){
    // Do something
  }
}
Clone this wiki locally