4

I have a requirement, wherein I have to skip the execution of trigger on Task after update operation on Task from Case. On update of case fields, am performing an update operation on Task Object. This fires Task triggers and LastModified by gets changed for all the Task records which are related to Case Object.

My intent is to skip the execution of TaskTriggerHandler method, only for this particular update operation when it happens on the case. Is it possible, if yes, please let me know....

3 Answers 3

7

You can implement that logic by yourself.

You can write an UtilClass to check if you execute or not the Trigger Logic

public class ByPassUtils {

    public static Map<String,Boolean> triggerNameBypassed = new Map<String,Boolean>();
    
    public static void ByPass(String triggerName){
        triggerNameBypassed.put(triggerName,true);
    }
    
    public static void UndoByPass(String triggerName){
        triggerNameBypassed.put(triggerName,false);
    }
    
    public static Boolean isByPassed(String triggerName){
        return triggerNameBypassed.containsKey(triggerName) && triggerNameBypassed.get(triggerName);
    }
}

logic when you want to bypass (maybe in the case trigger for your example)

ByPassUtils.ByPass('TaskAfterUpdate');

in your trigger code (in your case TaskAfterUpdate)

if(!ByPassUtils.isByPassed('TaskAfterUpdate')){
    // your logic here
}
3

There an easy practical way to do something like this

In your Task trigger add something like this

public class TriggerControl {
    public static Boolean isComingFromCase = false;
}

trigger TaskTrigger on Task (..all your before/afters) {

  public void MyTriggerMethod(...your params) {
    if (TriggerControl.isCallingFromCase) {
      return;
    }

    ...rest of your code
  }


}

Then, on whatever method you want to skip the triggers you can do

...code

TriggerControl.isComingFromCase = true;

...more code

If you set that variable to true before you perform the update that kicks off the Task Trigger then the methods for which you added the if statement won't be executed.

8
  • 1
    At least mention handler pattern! :P
    – Adrian Larson
    Jul 28, 2016 at 15:22
  • You just did. :) Jul 28, 2016 at 15:22
  • 1
    It’s a trigger. You can’t create it dynamically. Also, its methods are static so there is no need for an instance of the class. May 19, 2019 at 19:57
  • 1
    I’m not sure what the error could be. If you updated your code with my recent correction and it still doesn’t work then I’d open another question. You should reference this one so it doesn’t get closed as a dupe. May 19, 2019 at 20:03
  • 1
    @S.B. I noticed that in your new question you found a problem with my original answer. I've updated my code to reflect the fix to the problem May 21, 2019 at 0:46
2

When you are updating a particular field on case you update a particular field on task which fires another trigger on Task object to update the last modified by.

You can put condition on particular field.

If(Case.someFieldValue == 'someValue'){

//Then only update the task field.

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .