Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This commit fixes issue 86 #98

Merged
merged 3 commits into from
Mar 18, 2016
Merged

Conversation

Autobat
Copy link
Contributor

@Autobat Autobat commented Jan 6, 2016

Simple interface to be able to disable/enable trigger events from running.

This common code is repeated many times in many instances of SFDC. Where programatically we wish to prevent one, many or all trigger events firing for a domain. There are many reasons why this is required and this allows the developer to easily control which events are fired by using:

fflib_SObjectDomain.getTriggerEvent(Accounts.class).enableAll();  // this is the default behaviour

...
// mass adjustment
.disableAll();
.disableAllBefore();
.enableAllAfter();
.disableAllAfter();

// singular
.enableBeforeInsert();
.disableBeforeInsert();

... etc

Currently the class is called EventTrigger which i'm not sold on, if anyone can think of a better name I am happy to change.

Cheers

Starting to privide an ability to turn trigger methods on and off - this will enable people to stop cyclic triggers e.g.

within an Account domain, if an after insert trigger handler is creating another Account you can shut the infintate loop down by putting the following in your onAfterInsert method

getTriggerEvent(Accounts.class).disableAll();
@Autobat Autobat mentioned this pull request Jan 6, 2016
@Autobat
Copy link
Contributor Author

Autobat commented Jan 20, 2016

let me know if this requires any tweaking or modifications. I have a whole lot of flying and waiting in airports over the coming weeks!

@afawcett
Copy link
Contributor

Ha about to start a bit of the same myself. WIll take a look, thanks! 👍

afawcett added a commit that referenced this pull request Mar 18, 2016
This commit fixes issue 86
@afawcett afawcett merged commit 0ab6db6 into apex-enterprise-patterns:master Mar 18, 2016
@afawcett
Copy link
Contributor

Great contribution! Not sure if you have a blog or not, wondered if you wanted to write a short blog on why you contributed this, your motivations and some example code? BTW, love the use of the fluent API style! ;-)

@Autobat
Copy link
Contributor Author

Autobat commented Mar 22, 2016

I don't have a blog but great idea. I can write a piece and we can find someone to host as a guest entry.

Chris Mail

On 19 Mar 2016, at 02:10, Andrew Fawcett notifications@github.com wrote:

Great contribution! Not sure if you have a blog or not, wondered if you wanted to write a short blog on why you contributed this, your motivations and some example code? BTW, love the use of the fluent API style! ;-)


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

@Autobat
Copy link
Contributor Author

Autobat commented Apr 4, 2016

@afawcett is this what you're looking for?

How to put the safety on.

Being an architect in a professional services organisation is a funny game. Each project is either a shiny new Salesforce instance without a fingerprint on it or an unknown vault of code and configuration that we must navigate through.

I have been using the fflib pattern now for some time, and more of our teams are adopting it for our programs of work. My latest addition is something that an architect might wonder why we need; the ability to turn off triggers via a simple interface on all domains.

In an ever growing complex environment, perhaps multiple projects over time delivering iterative enhancements I was noticing a common piece of code being developed within the Domain layer. It looked something along the lines of this:

public override void onAfterInsert()
{
            // if this is set we are already in a loop and want to exit!
            if(bProhibitAfterInsertTrigger)
            {
                        return;
            }

            // down here we do something, maybe insert an Account!
}

While small and inconspicuous it allowed our code base to become inconsistent as there was no control over the exposure of these controlling flags and worse, we were repeating ourselves in every domain!

The solution was simple, a fluent style API within fflib_SObjectDomain. Any code can now simply set the control flags for any domain class:

fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAll(); // dont fire anything
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAllBefore();
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAllAfter();

fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableBeforeInsert();
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableBeforeUpdate();
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableBeforeDelete();

fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterInsert();
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterUpdate();
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterDelete();
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterUndelete();

To enable, just call the inverse e.g. .enableAfterInsert(); etc.

While not every code base will need to use these flags, they allow you to control quickly and easily your trigger execution with a single line of code that all your development team can reuse and follow.

@afawcett
Copy link
Contributor

Spot on! Nice work. 👍

Have you found a place to host it, happy to host it for you if not.

Once its up, i'll tweet and promote for ya.

Thanks again! 👍

@Autobat
Copy link
Contributor Author

Autobat commented Apr 13, 2016

Happy for it to end up on your blog

@afawcett
Copy link
Contributor

Done! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants