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
- From Setup, enter "Custom Metadata Types" in the Quick Find box and select Custom Metadata Types
- Click New Custom Metadata Type
- 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
- Click Save
2. Add Custom Fields
Add the following custom fields to the AOF_TriggerConfiguration__mdt metadata type:
-
SObject API Name
- Field Label: SObject API Name
- Field Name: SObjectApiName
- Field Type: Text
- Length: 255
- Required: Yes
-
Active
- Field Label: Active
- Field Name: IsActive
- Field Type: Checkbox
- Default Value: Checked (true)
-
Before Insert
- Field Label: Before Insert
- Field Name: BeforeInsert
- Field Type: Checkbox
- Default Value: Checked (true)
-
After Insert
- Field Label: After Insert
- Field Name: AfterInsert
- Field Type: Checkbox
- Default Value: Checked (true)
-
Before Update
- Field Label: Before Update
- Field Name: BeforeUpdate
- Field Type: Checkbox
- Default Value: Checked (true)
-
After Update
- Field Label: After Update
- Field Name: AfterUpdate
- Field Type: Checkbox
- Default Value: Checked (true)
-
Before Delete
- Field Label: Before Delete
- Field Name: BeforeDelete
- Field Type: Checkbox
- Default Value: Checked (true)
-
After Delete
- Field Label: After Delete
- Field Name: AfterDelete
- Field Type: Checkbox
- Default Value: Checked (true)
-
After Undelete
- Field Label: After Undelete
- Field Name: AfterUndelete
- Field Type: Checkbox
- Default Value: Checked (true)
-
Execution Order
- Field Label: Execution Order
- Field Name: ExecutionOrder
- Field Type: Number
- Length: 18
- Decimal Places: 0
- Default Value: 0
-
Environment
- Field Label: Environment
- Field Name: Environment
- Field Type: Text
- Length: 50
- Required: No
-
Description
- Field Label: Description
- Field Name: Description
- Field Type: Text
- Length: 255
- Required: No
-
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
- From Setup, enter "Custom Metadata Types" in the Quick Find box and select Custom Metadata Types
- Click Manage Records next to AOF Trigger Configuration
- Click New to create a new configuration record
- Fill in the fields as needed for each SObject you want to configure
- Click Save
Usage Examples
Example 1: Disable All Triggers for an SObject
To completely disable triggers for an SObject (e.g., Account):
- Create a new AOF_TriggerConfiguration__mdt record
- Set SObjectApiName__c = "Account"
- Set IsActive__c = false
- 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):
- Create a new AOF_TriggerConfiguration__mdt record
- Set SObjectApiName__c = "Contact"
- Set IsActive__c = true
- Set AfterUpdate__c = false
- Save the record
Example 3: Environment-Specific Configuration
To have different configurations for different environments:
- Create a new AOF_TriggerConfiguration__mdt record for production
- Set SObjectApiName__c = "Opportunity"
- Set Environment__c = "Production"
- Configure other fields as needed
- Create another record for the same SObject but with Environment__c = "Sandbox"
- Configure different settings for the sandbox environment
How It Works
The AOF_TriggerHandler now checks the metadata configuration before executing any trigger logic:
- When a trigger fires, the handler first checks if the SObject is bypassed programmatically
- If not bypassed, it checks if the SObject is active in the metadata configuration
- If active, it checks if the specific trigger event (e.g., before insert) is enabled
- 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
- Default Behavior: If no configuration exists for an SObject, triggers will be active by default
- Environment-Specific Settings: Use the Environment__c field to create different configurations for production, sandbox, and development environments
- Execution Order: Use the ExecutionOrder__c field to control the sequence when multiple handlers exist for the same SObject
- Documentation: Always fill in the Description__c field to document the purpose of each configuration
- 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:
- Adding custom fields to the AOF_TriggerConfiguration__mdt metadata type
- Enhancing the AOF_TriggerConfigurationService to support additional features
- Creating a custom admin UI to manage configurations more easily
Troubleshooting
If triggers are not behaving as expected:
- Verify that the AOF_TriggerConfiguration__mdt record exists for the SObject
- Check that IsActive__c is set to true
- Ensure the specific trigger event (e.g., BeforeInsert__c) is enabled
- Reset the configuration cache:
AOF_TriggerConfigurationService.resetCache();
- Check for any programmatic bypass:
AOF_TriggerHandler.isBypassed(Account.SObjectType)