Trigger Examples


template

/*
    Organization :  CortezLabs
    Created Date :  August 2019
    Created By :    Scott Grimes

    Description : 
        Account trigger
*/

trigger triggerTemplate on SObject (before insert, before update, before delete, after insert, after update, after delete)
{
    // ------------------------------------------------------------------------------------------------
    // Get the trigger settings
    ApexUtil apexUtilClass = ApexUtil.getInstance(null, 'Account');
    Map<String, Boolean> triggerSettings = apexUtilClass.objectTriggerSettings.get('Account');


    // ------------------------------------------------------------------------------------------------
    // Default system variables
    Boolean isInsert = Trigger.isInsert;
    Boolean isUpdate = Trigger.isUpdate;
    Boolean isDelete = Trigger.isDelete;
    Boolean isBefore = Trigger.isBefore;
    Boolean isAfter = Trigger.isAfter;
    DMLObject masterObject = new DMLObject();
    List<Account> newRecords = (isDelete ? Trigger.old : Trigger.new);
    Map<Id, Account> oldRecordMap = (isUpdate || isDelete ? Trigger.oldMap : Trigger.newMap);

    // ------------------------------------------------------------------------------------------------
    // Class declaration
    

    // ------------------------------------------------------------------------------------------------
    // Trigger.new value looping
    for(Account newRecord : newRecords)
    {
        // Old record
        Account oldRecord = (isUpdate ? oldRecordMap.get(newRecord.Id) : newRecord);

        // Call the methods
        if(!isDelete && isBefore)
        {

        }

        if(isDelete && isBefore)
        {

        }

        if(isAfter)
        {
            
        }
    }


    // ------------------------------------------------------------------------------------------------
    // After triggers run methods
    if(isAfter)
    {

    }


    // ------------------------------------------------------------------------------------------------
    // Before trigger run methods
    if(isBefore)
    {
        for(Account newRecord : newRecords)
        {
            // Old record
            Account oldRecord = (isUpdate ? oldRecordMap.get(newRecord.Id) : newRecord);

            // Call the methods
        }
    }


    // ------------------------------------------------------------------------------------------------
    // DML operations
    masterObject.dmlSObjectRecords();
}
 

class & method example

 
public without sharing class Account_ShortDescription
{
    Set<Id> accountIds = new Set<Id>();
    public Account_ConvertInsightContact() {
        accountIds = new Set<Id>();
    }

    /*  =============================================================================================================
        Created By: 
        Created Date: 
        Description: 
    */
    public void Check_ShortDescription(Account newAccount, Account oldAccount, Boolean isUpdate, Boolean triggerIsOn)
    {
        triggerIsOn = (triggerIsOn == NULL ? FALSE : triggerIsOn);
        if(!triggerIsOn & !Test.isRunningTest()) return;

        if(runConditions) {
            
        }
    }
    public DML_Object Run_ShortDescription(DML_Object masterobject)
    {
        if(accountIds.isEmpty()) return masterObject;
        
        // Run query
    }
}
 

 

CheckRun example

public without sharing class Opportunity_ShortDescription
{
    public Opportunity_ShortDescription() {}
    /*  ==========================================================================================================
        Description: 
    */
    public void CheckRun_ShortDescription(Opportunity newOpp, Opportunity oldOpp, Boolean isInsert, Boolean isUpdate, Boolean triggerIsOn)
    {
        // Check to see if the trigger is turned on (or is a test)
        triggerIsOn = (triggerIsOn == NULL ? FALSE : triggerIsOn);
        if(!triggerIsOn & !Test.isRunningTest()) return;

        if(runConditions)
        {
            // Process to run if conditions are met
        }
    }
}