function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alexander the KeeperAlexander the Keeper 

Triggers + workflows

Hello,

i need help with best practices from experienced salesforce developers.

For Update our Object we have Process Builder, Worfklow Rules and Triggers.

But we also know that if Process Builder or WFR have to change Object, then all triggers will be executed one more time.

So my example is:
I have Case Object
It already have 10-15 update triggers (before + after) (actually it's only 1 trigger but with a lot of other methods to run)
So i can see in debug log, that full work of this triggers, for example take 30 of 100 SOQL Querrys

Now i need to Stop Entitlement process for "Pending" Cases. (or another simple task, what can be accomplished with WFR/PB)
Ok. i have 2 ways:
1. Add WFR/PB with start/stop case for case status is changed = true && status = pending, etc.
2. Add more logic in Before Trigger.

And if i will use first way, then my triggers will be exucuted twice.
So, instead of 30 of 100 SOQL querries it will be 60 of 100... And for me it looks awful.

Main question: Am I right that if we already have complex structure with triggers, then using WFR/PB is undesirable (if they are update same object)?

Sorry for my english.
Best Answer chosen by Alexander the Keeper
Paul_BoikoPaul_Boiko
Hi Alexander,
It's fine to use triggers with Workflow Rules. In our org we have very complex triggers and many Workflow rules and PB. 
If you don't want triggers to run again in the same context you need to use static variable and and add if statement in your trigger to run code only if it runs first time in this context. Here is example:
In Apex class you need to add boolean static variable which set to true by default:
public class TriggerHandler{
     public static Boolean isFirstTime = true;
}
and then in your trigger or hendler class:
trigger CaseTrigger on Case (after update, before update){
    // if code runs first time in this context 
    if(TriggerHandler.isFirstTime){
        TriggerHandler.isFirstTime = false;
         
        for(Case c : Trigger.New){
            // ...
        }
         
    }
}

As a common practice, if your question is answered, please choose 1 best answer.
Also don't forget to give answer a thumb up if that answer helped you.