0

How to combine two triggers in same Trigger:

trigger createorderpricebook on Order (after update) {

    List<Order> orderList = new List<Order>();
    for(Order o : [SELECT Id,Status, Pricebook2Id,(SELECT Id FROM Order_Pricebooks__r) FROM Order WHERE Id IN :trigger.new]){

        if(o.Order_Pricebooks__r.isEmpty()){
            orderList.add(o);
        }
    }

    List<Order_Pricebook__c> orderPriceBookList = new List<Order_Pricebook__c>();


    for(Order ce:orderList) {

        if(ce.status =='Activated'){
            Order_Pricebook__c gh = new Order_Pricebook__c();
            gh.name= 'ela';
            gh.Order__c = ce.id;
            gh.Price_Book__c = ce.pricebook2Id;
            orderPriceBookList.add(gh);
        }
    }

    insert orderPriceBookList;
}

2.

trigger updateorderpricebook on Order (after update) {
    List<Order_Pricebook__c> orderPriceBookList = new List<Order_Pricebook__c>();

    for(Order ce:trigger.new) {
        list<Order_Pricebook__c> tt = [select id, Price_Book__c,Order__c from Order_Pricebook__c where Order__c =:ce.id];
             for(Order_Pricebook__c io:tt)
             {
              if(ce.status =='Activated')
          {
            io.Price_Book__c = ce.priceBook2Id;
            orderPriceBookList.add(io);
        }
}
    }
   update orderPriceBookList;
    }
1
  • Both of your triggers have same event then you can use single trigger and writing the logic in the handler would be better. Jul 21, 2016 at 12:26

3 Answers 3

2

You can use Trigger context variables.

trigger OrderPriceBookTrigger on Order (before update, after update){

  if (Trigger.isBefore) {
      // your before update trigger logic goes here
  }
  if (Trigger.isAfter) {
      // your after update trigger logic goes here  
  }

}
3
  • both triggers possible in after update how?
    – user30475
    Jul 21, 2016 at 5:47
  • You don't have to have two triggers. There will only one trigger as in code sample, firing for both before and after contexts. Check the reference link as well.
    – highfive
    Jul 21, 2016 at 5:49
  • Refer above code you will understand
    – user30475
    Jul 21, 2016 at 5:51
0

You'll need to delete those if you want to keep a nice naming convention, then create a single trigger like i did below:

//Trigger for before update and before insert    
// seperating types by comma in trigger creation line
trigger PriceBookTrigger on Order (before update,After update) {        
    //if we're in before update
    if (Trigger.isBefore && Trigger.isUpdate) {
        //Do before update logic
    }
    //if we're in after update
    if (Trigger.isAfter && Trigger.isUpdate) {
        //Do after Update logic
    } 
}  

Refer to this document for more info on trigger context variables: Trigger Context Variables

1
  • both are after update
    – user30475
    Jul 21, 2016 at 5:54
0

Then you can do something like to combine both triggers.

trigger createorderpricebook on Order (after update) {
    List<Order_Pricebook__c> orderPriceBookList = new List<Order_Pricebook__c>();
    List<Order> orderList = new List<Order>();
    for(Order o : [SELECT Id,Status, Pricebook2Id,(SELECT Id FROM Order_Pricebooks__r) FROM Order WHERE Id IN :trigger.new]){

        if(o.Order_Pricebooks__r.isEmpty()){
            if(o.status =='Activated'){
            Order_Pricebook__c gh = new Order_Pricebook__c();
            gh.name= 'ela';
            gh.Order__c = o.id;
            gh.Price_Book__c = o.pricebook2Id;
            orderPriceBookList.add(gh);
           }
        }
        else
         {
            for(Order_Pricebook__c io:o.Order_Pricebooks__r)
             {
              if(o.status =='Activated')
              {
                io.Price_Book__c = o.priceBook2Id;
                 orderPriceBookList.add(io);
              }
            }
      }
    }

    upsert orderPriceBookList;
}

You must log in to answer this question.