Disabling triggers without deactivating them
There are various scenarios in which we faced with the decision of disabling triggers. When mass loading data into an object, e.g. loading legacy data from another system onto Salesforce or when restoring data from a backup database, disabling the triggers ensures the data is not modified in unexpected ways.
Disabling a trigger however, comes with other consequences — for instance what happens to new data created through other channels whilst the mass upload is ongoing or while data from a backup solution is being restored. For instance, if new cases rely on the case trigger logic to be routed to the right customer care agents, the cases created during the downtime may not be routed on time or missed altogether, failing to satisfy any customer SLA requirements that may be in place.
Depending on the criticality of this data, disabling the trigger might not be an option.
So, how can we avoid running the triggers without disabling them?
The simple answer is, switch off the trigger for the user running the mass upload/restore.
Here’s a pattern that would allow this to happen without affecting other users of the system.
The variable skipTrigger can be set to true to skip any trigger logic. Rather than hardcoding this value, it can be read from a hierarchical custom setting that can hold different values for different users/profiles.
Here, the custom setting (Trigger switch) allows the trigger logic to be skipped for the System Administrator profile users. Equally by having an entry for each user for whom we would like the trigger logic to be skipped, the trigger logic can be skipped for these specific users as shown below.