Skip to content

Trigger framework for Apex simplifying the trigger handlers dispatching

License

Notifications You must be signed in to change notification settings

IlyaMatsuev/Apex-Trigger-Framework

Repository files navigation

Apex Trigger Framework

Scratch org CI codecov

Simple Apex Trigger Framework wrapped as an unlocked package. Contains the following features:

  • Delegating trigger handlers
  • Exchanging parameters between all handlers
  • Skiping mechanism
  • Configuring trigger error handlers
  • Binding asynchronous trigger handlers (Queueable)

Overview

To create a trigger handler you first need to implement the Triggers.IHandler interface:

public class TestTriggerHandler implements Triggers.IHandler {
    public void handle(Triggers.Context context) {
        // Sample logic
        Integer count = context.stash.containsKey('count') ? (Integer) context.stash.get('count') : 0;
        context.stash.put('count', ++count);
        System.debug('count: ' + count);
    }
}

The example of using the framework:

trigger Account on Account (before insert, before update, before delete, after insert, after update, after delete) {
	Triggers.dispatcher
        .bind(TriggerOperation.AFTER_INSERT, new TestTriggerHandler())
        .bind(TriggerOperation.AFTER_UPDATE, new TestTriggerHandler())
        .bindAsync(TriggerOperation.AFTER_DELETE, new TestAsyncTriggerHandler())
        .run();
}

First, setup all your handlers by calling bind() or bindAsync() methods from the Triggers.dispatcher instance. After all handlers set you call the run() method.

Installation

You can just install the package by the link on a sandbox or dev org.

If you prefer using salesforce CLI you can run:

sfdx force:package:install -p 04t5Y000001wNArQAM -w 10 -b 10 -u <username>

Documentation

For more detailed information about the content of the repository and the sfdx package, please visit the docs folder.

Questions

If you have any questions you can start a discussion.
If you think something works not as expected or you want to request a new feature, you can create an issue with the appropriate template selected.

Contributing

Pull requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.

License

MIT

About

Trigger framework for Apex simplifying the trigger handlers dispatching

Topics

Resources

License

Stars

Watchers

Forks

Languages