Action Apex
The Apex part of the action contains the business logic associated with said action.
TriggerAction interface
Any Apex trigger action which is to be used within the framework must implement the seven20.TriggerAction interface. This interface defines the following methods:
void run(
List<SObject> oldList,
List<SObject> newList,
Map<Id, SObject> oldMap,
Map<Id, SObject> newMap,
System.TriggerOperation operationType
)
The input parameters are derived from the trigger context, and are passed to the action by the framework, these are derived as follows:
| Parameter | Source | Type |
|---|---|---|
| oldList | Trigger.old | List<SObject> |
| newList | Trigger.new | List<SObject> |
| oldMap | Trigger.oldMap | Map<Id, SObject> |
| newMap | Trigger.newMap | Map<Id, SObject> |
| operationType | Trigger.operationType | System.TriggerOperation |
Boilerplate code
Below is the boilerplate code, required for a basic trigger action:
global with sharing class ExampleTriggerAction implements seven20.TriggerAction{
global void run(
List<SObject> oldList,
List<SObject> newList,
Map<Id, SObject> oldMap,
Map<Id, SObject> newMap,
System.TriggerOperation operationType
){
// Business logic goes here
}
}
danger
The action and run methods must both use the global access modifier.
Implementation guidelines
- Actions should be standalone and focused. If an action is required to perform multiple tasks, consider splitting it into multiple actions.
- Actions should be idempotent, i.e. repeated calls to it in a transaction should not cause issues.
- Actions should be bulkified, i.e. they should be able to handle multiple records in a single transaction.
- Actions should be stateless, i.e. they should not rely on any state being maintained between invocations.
- Action invocation can be mocked during unit testing, i.e. the action can be directly invoked with test data, rather than relying on the trigger framework to invoke it.
- Using gating criteria to determine whether an invoked action needs to perform operations on a record, i.e. checking if a relevant field has changed
tip
If exceptions occur during the execution of an action, ensure a meaningful error message is returned to the user.