Skip to main content

Trigger Action Framework (TAF)

The Trigger Action Framework (TAF) provides a global Apex class and custom metadata types for you to configure trigger logic with, including turning trigger functions on and off as well as configuring the order in which each trigger runs. The framework can be used with both Apex classes and flows and works for both Komodo Publications Planning code and custom code, such as standard objects, custom objects, and objects from installed packages.

Enable a trigger

To enable a trigger on an sObject:

  1. Call the Dynamic Trigger Handler (TAF_DynamicTriggerHdlr) global Apex class within the trigger body of the sObject and pass a running key to the run(key) method. The running key is designed to prevent duplicate calls, and Komodo Health recommends setting the running key to be name of the trigger handler.

  2. Create a Trigger sObject Setting (mvn__TAF_sObject_Trigger_Setting__mdt) metadata record for the sObject.

    • If the sObject is from an installed package, separate the namespace from the API name of the object and set the Object Namespace (mvn__TAF_Object_Namespace__c) and Object API Name (mvn__TAF_Object_API_Name__c) fields accordingly.

  3. Complete the steps in the sections below depending on if the trigger should call an Apex action or a flow action.

Trigger an Apex action

If the trigger should call an Apex class:

  1. Write an Apex class that defines the trigger action.

  2. Create a Trigger Action (mvn__TAF_Trigger_Action__mdt) metadata record for the action that should be executed on the sObject and the context in which it executes.

    1. Set the Apex Class Name (mvn__TAF_Apex_Class_Name__c) field to the name of the Apex class.

    2. Set the Apex Class Namespace (mvn__TAF_Apex_Class_Namespace__c) field to the namespace of the Apex class.

    3. Set the Order (mvn__TAF_Order__c) field as appropriate.

Trigger a flow action

If the trigger should invoke a flow:

  1. Create an autolaunched flow that defines the trigger action with the following flow resource variables:

    Table 142. Autolaunched flow resource variables

    Variable name

    Variable type

    Available for input

    Available for output

    Available contexts

    Description

    record

    record

    yes

    yes

    • insert

    • update

    • undelete

    The new version of the record in the DML operation.

    recordPrior

    record

    yes

    no

    • update

    • delete

    The old version of the record in the DML operation.



  2. Create a Trigger Action (mvn__TAF_Trigger_Action__mdt) metadata record for the action that should be executed on the sObject and the context in which it executes.

    1. Set the Apex Class Name (mvn__TAF_Apex_Class_Name__c) field to TAF_TriggerActionFlow.

    2. Set the Flow Name (mvn__TAF_Flow_Name__c) field to the API name of the flow.

    3. Set the Order (mvn__TAF_Order__c) field as appropriate.

    4. (Optional) Check the Allow Flow Recursion? (mvn__TAF_Allow_Flow_Recursion__c) checkbox (i.e., set to true) to run the flow recursively.

Bypass a trigger or action

Triggers can be bypassed on entire sObjects, by specific actions, and by users and profiles and can be controlled through both custom metadata records and code.

Bypass with custom metadata records

To bypass all triggers on a single sObject, check the Bypass Execution (mvn__TAF_Bypass_Execution__c) checkbox (i.e., set to true) on the respective Trigger sObject Setting (mvn__TAF_sObject_Trigger_Setting__mdt) metadata record. To bypass all instances of a trigger action, check the Bypass Execution (mvn__TAF_Bypass_Execution__c) checkbox (i.e., set to true) on the respective Trigger Action (mvn__TAF_Trigger_Action__mdt) metadata record. The bypasses will remain active until the Bypass Execution field for each metadata record is unchecked.

Warning

You may bypass any default Trigger Action metadata records to disable product trigger actions. However, doing so can, and will, affect product functionality.

To bypass or execute triggers and actions by users or profiles, use the Bypass Permission (mvn__TAF_Bypass_Permission__c) and Required Permission (mvn__TAF_Required_Permission__c) fields on the Trigger sObject Setting and Trigger Action custom metadata types. The former uses a custom permission to determine if a trigger or action should be bypassed for the running user while the latter uses a custom permission to determine if a trigger or action should execute for the running user.

Bypass with code

To bypass and execute triggers with code, call the appropriate Dynamic Trigger Handler (TAF_DynamicTriggerHdlr) global methods in your custom Apex classes and flows. Any bypasses will remain active until the bypass flags are cleared.

DML finalizer

A Data Manipulation Language (DML) finalizer is a block of code that executes exactly one time at the very end of a DML operation. DML finalizers are helpful in scenarios such as enqueuing a queueable operation or inserting a collection of gathered logs and can be called with the TAF as well.

To call a DML finalizer:

  1. Write an Apex class that defines the DML finalizer.

    Note

    Keep the following in mind when creating the DML finalizer:

    • The finalizer cannot call any other DML operations. Otherwise, it may not execute at the very end of the operation and a runtime error may be thrown.

    • All configured finalizers will be invoked at the end of the DML operation, regardless of the sObject that triggered the operation.

  2. Create a Trigger Finalizer (mvn__TAF_DML_Finalizer__mdt) metadata record.

    1. Set the Apex Class Name (mvn__TAF_Apex_Class_Name__c) field to the name of the Apex class.

    2. Set the Apex Class Namespace (mvn__TAF_Apex_Class_Namespace__c) field to the namespace of the Apex class.

    3. Set the Order (mvn__TAF_Order__c) field as appropriate.

The DML finalizer can also be bypassed the same way as the other triggers and actions.