Trigger Examples


template

trigger triggerTemplate on SObject (before insert, before update, before delete, after insert, after update, after delete)
{
    // Trigger Settings ===========================================================================================
    Map<String, Boolean> taskTrigSettings = new Map<String, Boolean>();
    for(Trigger_Setting__mdt trig : [select MasterLabel, is_Active__c from Trigger_Setting__mdt where Object__c = '__object name__']) {
        taskTrigSettings.put(trig.MasterLabel, trig.is_Active__c);
    }

    DML_object masterObject = new DML_object();

    // Methods ====================================================================================================
    

    // Loop Through Changed Tasks
    List<SObject> newRecods = (Trigger.isDelete ? Trigger.old : Trigger.new);
    Map<Id, SObject> oldTaskMap = (Trigger.isUpdate || Trigger.isDelete ? Trigger.oldMap : Trigger.newMap);
    Map<Id, SObject> newTaskMap = (Trigger.isDelete ? Trigger.oldMap : Trigger.newMap);
    for(SObject newRecord : newRecods) {
        SObject oldRecord = (Trigger.isUpdate || Trigger.isDelete ? oldTaskMap.get(newRecord.Id) : newRecord);
        
        // Methods
        if(Trigger.isBefore) {
            
        }
        if(Trigger.isAfter) {
            
        }
    }
    // After trigger query methods
    if(Trigger.isAfter) {

    }
    // Before trigger updates
    if(Trigger.isBefore) {
        // Before trigger query methods

        
        for(SObject newRecord : newRecords) {
            SObject oldRecord = (Trigger.isUpdate || Trigger.isDelete ? oldTaskMap.get(newRecord.Id) : newRecord);

            // Methods
        }
    }
    // Sort and Upsert
    masterObject.SortAndUpsert();
}

class & method

Check, Query, & Run 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
        }
    }
}