fflib-apex-common
fflib-apex-common copied to clipboard
Disabling triggers using a custom setting
Hello @afawcett,
Hope you are well. So, I have a project where I need to give client salesforce.com admin control to enable/disable triggers by using a custom setting. For e.g. there are triggers on Account, Opportunity and Contact. I want to give Administrator to enable disable triggers using a custom setting with fields like these:
- Disable All Triggers (disables all apex triggers across the org)
- Disable Opportunity Trigger (disables all Opportunity triggers)
- Disable Opportunity Creat Invoices (disables a particular method in Opportunities SObjectDomain class)... and so on for Account and Contact objects.
Can you please advise on where should I keep the additional logic for disabling all triggers, particular object trigger disabling, and also if possible how to control a particular method (functionality) of a trigger? Ideally, I don't want to make changes in fflib_SObjectDomain class for keeping it upgradable from this project.
Let me know what you think would be the best way to do it.
Thanks, Mustafa
Hey @mustafaturabali
I would check out:
https://andyinthecloud.com/2016/04/13/disabling-trigger-events-in-apex-enterprise-patterns/
The pattern you are looking for is already within fflib and exposed as a fluent api. You can programmatically turn off the execution of any trigger event.
Let me know how you get on or if you need any help,
Cheers,
Chris
Hello Chris,
This is what I have done so far:
public static TriggerEvent getTriggerEvent(Type domainClass) { if (!TriggerEventByClass.containsKey(domainClass)) { TriggerEvent objTriggerEvent = new TriggerEvent(); if(EJ_Settings__c.getOrgDefaults().Disable_All_Triggers__c) { objTriggerEvent.disableAll(); } TriggerEventByClass.put(domainClass, objTriggerEvent); }
return TriggerEventByClass.get(domainClass);
}
I have modified this method in fflib_SObjectDomain class to disable all events for any trigger being called using custom settings field.
Let me know if you have a better solution in mind.
Regards, Mustafa
On 01-Jul-2016, at 9:07 AM, Chris Mail [email protected] wrote:
Hey @mustafaturabali https://github.com/mustafaturabali I would check out:
https://andyinthecloud.com/2016/04/13/disabling-trigger-events-in-apex-enterprise-patterns/ https://andyinthecloud.com/2016/04/13/disabling-trigger-events-in-apex-enterprise-patterns/ The pattern you are looking for is already within fflib and exposed as a fluent api. You can programmatically turn off the execution of any trigger event.
Let me know how you get on or if you need any help,
Cheers,
Chris
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/financialforcedev/fflib-apex-common/issues/126#issuecomment-229849465, or mute the thread https://github.com/notifications/unsubscribe/ABQdvySZ63bgEBxN5FzYXAnBLfuPCMCVks5qRJKUgaJpZM4JCyHC.
Hey Mustafa,
Sorry for the delay, i have been offline with the flu.
That looks ok. It would allow a developer to reenable the triggers programmatically as this only gets set on the first invocation - this might be beneficial depending on your use case.
Its a shame @mustafaturabali had to modify the code to achieve this. As this will impact future upgrading of this library. Any thoughts on how to avoid this?
It would be possible if we created some core fflib custom settings.
Would this be something the library wanted? It would be something quite simple.
Let me think about it and propose a solution to the group.
Chris Mail Director +61 424 022 071
On 15 Jul 2016, at 19:40, Andrew Fawcett [email protected] wrote:
Its a shame @mustafaturabali had to modify the code to achieve this. As this will impact future upgrading of this library. Any thoughts on how to avoid this?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
Hello,
Just to add my two cents.. May be we can provide an interface/virtual method like isEnabled() and leave the implementing class to handle it? This will let developers provide their own implementation of how they wanna handle it i.e. using custom settings or some criteria based enabling/disabling of certain triggers.
Mustafa
Sent from my iPhone
On Jul 15, 2016, at 4:43 PM, Chris Mail [email protected] wrote:
It would be possible if we created some core fflib custom settings.
Would this be something the library wanted? It would be something quite simple.
Let me think about it and propose a solution to the group.
Chris Mail Director +61 424 022 071
On 15 Jul 2016, at 19:40, Andrew Fawcett [email protected] wrote:
Its a shame @mustafaturabali had to modify the code to achieve this. As this will impact future upgrading of this library. Any thoughts on how to avoid this?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
Any pull request for that?
@mustafaturabali "May be we can provide an interface/virtual method like isEnabled() and leave the implementing class to handle it?" is that not what is already provided per what @Autobat was saying above?
Hey @afawcett @Autobat ,
Have we enabled any feature update to this? I am thinking of custom metadata to have granular control over the functionality. In the fflibDomain, we can have the defined custom metadata and use the map associate to enable and disable the feature [ Trigger Control By Metadata.pdf ](url)
@afawcett @Autobat
Did we upgrade the library to use the Custom Metadata or Custom Settings to Control the Trigger Events instead of writing them in apex code?
@pantherschools, have you checked out the AT4DX library which is deployed alongside the FFLIB Apex Common library? It controls domain processes via a custom metadata approach -- see Domain Process Injection
@ImJohnMDaniel I did deploy the FFLIB code yesterday only and I do not see any custom metadata deployed to my org.
The enablement of domain process injection via Custom Metadata comes with the addition of the AT4DX framework.
@pantherschools The AT4DX framework offers a great set of features, but if you find that too much there is also a lightweight alternative offered by the fflib-apex-extensions. Please review the wiki pages on that link which will explain how to use it.