Metadata‐Driven Trigger Configuration Guide - mouttaqui/AOF GitHub Wiki

Overview

The Apex Orbit Framework (AOF) now supports metadata-driven trigger configuration, allowing administrators to control trigger behavior without modifying code. This feature enables you to:

  • Enable or disable triggers for specific SObjects
  • Control which trigger events are processed (before insert, after update, etc.)
  • Set execution order when multiple handlers exist for the same SObject
  • Configure environment-specific behavior

Custom Metadata Type: AOF_TriggerConfiguration__mdt

Fields

Field Label API Name Data Type Description
SObject API Name SObjectApiName__c Text(255) API name of the SObject this configuration applies to (e.g., Account, Contact, Custom_Object__c)
Active IsActive__c Checkbox Controls whether triggers for this SObject are active
Before Insert BeforeInsert__c Checkbox Controls whether Before Insert trigger events are processed
After Insert AfterInsert__c Checkbox Controls whether After Insert trigger events are processed
Before Update BeforeUpdate__c Checkbox Controls whether Before Update trigger events are processed
After Update AfterUpdate__c Checkbox Controls whether After Update trigger events are processed
Before Delete BeforeDelete__c Checkbox Controls whether Before Delete trigger events are processed
After Delete AfterDelete__c Checkbox Controls whether After Delete trigger events are processed
After Undelete AfterUndelete__c Checkbox Controls whether After Undelete trigger events are processed
Execution Order ExecutionOrder__c Number(18, 0) Controls the order of execution when multiple handlers exist for the same SObject
Environment Environment__c Text(50) Optional field to specify which environment this configuration applies to (e.g., Production, Sandbox, Dev)
Description Description__c Text(255) Description of this configuration record
Custom Logic CustomLogic__c Long Text Area(32768) Optional field for storing JSON or other configuration data for custom logic

Setup Instructions

1. Create the Custom Metadata Type

  1. From Setup, enter "Custom Metadata Types" in the Quick Find box and select Custom Metadata Types
  2. Click New Custom Metadata Type
  3. Enter the following:
    • Label: AOF Trigger Configuration
    • Plural Label: AOF Trigger Configurations
    • Object Name: AOF_TriggerConfiguration
    • Description: Configuration settings for the Apex Orbit Framework trigger handler
  4. Click Save

2. Add Custom Fields

Add the following custom fields to the AOF_TriggerConfiguration__mdt metadata type:

  1. SObject API Name

    • Field Label: SObject API Name
    • Field Name: SObjectApiName
    • Field Type: Text
    • Length: 255
    • Required: Yes
  2. Active

    • Field Label: Active
    • Field Name: IsActive
    • Field Type: Checkbox
    • Default Value: Checked (true)
  3. Before Insert

    • Field Label: Before Insert
    • Field Name: BeforeInsert
    • Field Type: Checkbox
    • Default Value: Checked (true)
  4. After Insert

    • Field Label: After Insert
    • Field Name: AfterInsert
    • Field Type: Checkbox
    • Default Value: Checked (true)
  5. Before Update

    • Field Label: Before Update
    • Field Name: BeforeUpdate
    • Field Type: Checkbox
    • Default Value: Checked (true)
  6. After Update

    • Field Label: After Update
    • Field Name: AfterUpdate
    • Field Type: Checkbox
    • Default Value: Checked (true)
  7. Before Delete

    • Field Label: Before Delete
    • Field Name: BeforeDelete
    • Field Type: Checkbox
    • Default Value: Checked (true)
  8. After Delete

    • Field Label: After Delete
    • Field Name: AfterDelete
    • Field Type: Checkbox
    • Default Value: Checked (true)
  9. After Undelete

    • Field Label: After Undelete
    • Field Name: AfterUndelete
    • Field Type: Checkbox
    • Default Value: Checked (true)
  10. Execution Order

    • Field Label: Execution Order
    • Field Name: ExecutionOrder
    • Field Type: Number
    • Length: 18
    • Decimal Places: 0
    • Default Value: 0
  11. Environment

    • Field Label: Environment
    • Field Name: Environment
    • Field Type: Text
    • Length: 50
    • Required: No
  12. Description

    • Field Label: Description
    • Field Name: Description
    • Field Type: Text
    • Length: 255
    • Required: No
  13. Custom Logic

    • Field Label: Custom Logic
    • Field Name: CustomLogic
    • Field Type: Long Text Area
    • Length: 32768
    • Visible Lines: 5
    • Required: No

3. Create Configuration Records

  1. From Setup, enter "Custom Metadata Types" in the Quick Find box and select Custom Metadata Types
  2. Click Manage Records next to AOF Trigger Configuration
  3. Click New to create a new configuration record
  4. Fill in the fields as needed for each SObject you want to configure
  5. Click Save

Usage Examples

Example 1: Disable All Triggers for an SObject

To completely disable triggers for an SObject (e.g., Account):

  1. Create a new AOF_TriggerConfiguration__mdt record
  2. Set SObjectApiName__c = "Account"
  3. Set IsActive__c = false
  4. Save the record

Example 2: Disable Specific Trigger Events

To disable only specific trigger events for an SObject (e.g., disable after update for Contact):

  1. Create a new AOF_TriggerConfiguration__mdt record
  2. Set SObjectApiName__c = "Contact"
  3. Set IsActive__c = true
  4. Set AfterUpdate__c = false
  5. Save the record

Example 3: Environment-Specific Configuration

To have different configurations for different environments:

  1. Create a new AOF_TriggerConfiguration__mdt record for production
  2. Set SObjectApiName__c = "Opportunity"
  3. Set Environment__c = "Production"
  4. Configure other fields as needed
  5. Create another record for the same SObject but with Environment__c = "Sandbox"
  6. Configure different settings for the sandbox environment

How It Works

The AOF_TriggerHandler now checks the metadata configuration before executing any trigger logic:

  1. When a trigger fires, the handler first checks if the SObject is bypassed programmatically
  2. If not bypassed, it checks if the SObject is active in the metadata configuration
  3. If active, it checks if the specific trigger event (e.g., before insert) is enabled
  4. Only if all checks pass will the trigger logic execute

The configuration is cached for performance, but you can reset the cache if needed:

AOF_TriggerConfigurationService.resetCache();

Best Practices

  1. Default Behavior: If no configuration exists for an SObject, triggers will be active by default
  2. Environment-Specific Settings: Use the Environment__c field to create different configurations for production, sandbox, and development environments
  3. Execution Order: Use the ExecutionOrder__c field to control the sequence when multiple handlers exist for the same SObject
  4. Documentation: Always fill in the Description__c field to document the purpose of each configuration
  5. Custom Logic: Use the CustomLogic__c field to store JSON or other configuration data for complex scenarios

Extending the Framework

You can extend the metadata-driven configuration by:

  1. Adding custom fields to the AOF_TriggerConfiguration__mdt metadata type
  2. Enhancing the AOF_TriggerConfigurationService to support additional features
  3. Creating a custom admin UI to manage configurations more easily

Troubleshooting

If triggers are not behaving as expected:

  1. Verify that the AOF_TriggerConfiguration__mdt record exists for the SObject
  2. Check that IsActive__c is set to true
  3. Ensure the specific trigger event (e.g., BeforeInsert__c) is enabled
  4. Reset the configuration cache: AOF_TriggerConfigurationService.resetCache();
  5. Check for any programmatic bypass: AOF_TriggerHandler.isBypassed(Account.SObjectType)