1

I have a custom object in which i have two triggers.

Trigger 1( before insert and after update) Trigger 2(After Insert)

Currently whenever i insert a record, trigger 1 fires before i insert the record and trigger 2 fires after it gets inserted.

I tried consolidating the two triggers into 1 trigger . I used the Trigger.isBefore and Trigger.isAfter. The problem i run into is I used the Trigger.isBefore and Trigger.isAfter in if else loop. so it is picking wither one event or the other. But i want it to fire both after is.Before and isAfter for insert. is there way to do it in a single trigger or should i continue to have two triggers.

Appreciate the help in advance

************update*********** I have a recursive check on my triggers because it is going into a infinite loop till it hits the governor limits.so that is the reason i had it before insert events in trigger 1(it will execute only once per operation) and after insert events in trigger 2. I want to move trigger 2 code into trigger 1.

  • 1
    They will still both run if you use else statements... Have you observed differently? If so, please edit your post to include the trigger body. – Adrian Larson Jun 30 '16 at 21:54
  • 1
    Yes you can do both in single trigger. You must have had something else incorrect – Eric Jun 30 '16 at 21:54
  • I am sorry that i didn't inform this before hand. I have a recursive check on Trigger 1. So i would like to know when @vladykx mentioned that there will be two different calls to same trigger. I know this is silly question. Can someone explain to me whether a trigger will be called twice for the before events once and the after events once. Is this a true statement – Dino Jul 1 '16 at 14:00
1

As long as you include both before insert and after insert in the new trigger, both of them will fire, even when you use if/else to separate them, as there will be two different calls to the same trigger. I suggest to go over Order of Execution to understand how this works: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm If you look at the above link, you can see the trigger executed twice (or more in more complex scenario):

3. Executes all before triggers.
...
7. Executes all after triggers.
  • I am sorry that i didn't inform this before hand. I have a recursive check on Trigger 1. So i would like to know when you mentioned there will be two different calls to same trigger. I think that might be the problem – Dino Jul 1 '16 at 13:58
1

Actually I believe that using more than 1 trigger is not recommended. Also, it's best practice to have a trigger handler, which will do all the required logic, and not have the logic in the trigger. To answer your question: yes, you can(and should) only have 1 trigger.

An example:

Trigger:

trigger TRIG_Account on Account (before insert, before update, after insert, after update) {

    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            TRIG_Account_Handler.beforeInsert(Trigger.new);
            return;
        }
        if (Trigger.isUpdate) {
            TRIG_Account_Handler.beforeUpdate(Trigger.oldMap, Trigger.newMap);
            return;
        }
    }

    if (Trigger.isAfter) {
        if (Trigger.isInsert) {
            TRIG_Account_Handler.afterInsert(Trigger.new);            
            return;
        }
        if (Trigger.isUpdate) {
            TRIG_Account_Handler.afterUpdate(Trigger.oldMap, Trigger.newMap); 
            return;
        }
    }
}

Trigger handler(just an apex class):

public without sharing class TRIG_Account_Handler {

    // This will handle all before insert logic
    public static void beforeInsert(List<Account> newAccounts) {
        System.debug('In before insert!');
    }
    // This will handle all before update logic
    public static void beforeUpdate(Map<Id , Account> oldAccounts, Map<Id, Account> newAccounts) {
        System.debug('In before update!');
    }
    // This will handle all after insert logic
    public static void afterInsert(List<Account> newAccounts) {
        System.debug('In after insert!');
    }
    // This will handle all after update logic
    public static void afterUpdate(Map<Id , Account> oldAccounts, Map<Id, Account> newAccounts) {
        System.debug('In after update!');
    }
}

Using this, you can simply split all the logic in different methods.

0

i think there should not be any issue in this. you can put the if clause as per your need (using isbefore, isinsert etc), its just the way you build your logic. in fact you can do all your operations using a single trigger and even that is the best practice one should follow as this helps in keeping your code organised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.