0

Before update is fired when before insert is updating records of same object which is in turn triggering before update

trigger updateTestObject on TestObject__c (before insert, before update) { 

  if(Trigger.isbefore){ 
    if(trigger.isinsert){
      List<TestObject__c> updateList = new List<TestObject__c>();

      List<TestObject__c> lsttest = [Select Id, Name, Customfield1,Customfield2 
                                      from TestObject__c where Customfield1='Test'];
      for(TestObject__c ob1:trigger.new){
        for(TestObject ob2:lsttest){
          if(some condn){
            ob2.Customfield2 = 'some value';
            updateList.add(ob2);
          }
        }
      }
     update updateList;//this fires the update trigger
   }
   if(trigger.inUpdate){       
     for(TestObject__c obj: trigger.new){
        if(obj.Customfield2 != Null){
            //some logic
        }
      }
    }
  }
}

when the update trigger is firing, no value is found in obj.customfield2, i.e it is null, though it has been updated in the above insert trigger as ob2.customfield2 = 'some value' on insert of ob1 record.

my purpose is to not let this update event fire update trigger, butu it is not getting handled by recursion, since the transaction on the update become a new transaction. so to stop recursion I put a filter - obj.Customfield2 != Null which also is not working, as it comes out as null.

  • 4
    Can you try to make this more clear. I don't understand what the question actually is here. – Chris Duncombe Jun 3 '15 at 14:00
  • Not sure about the exact query, but are you trying to separate 'before insert' and 'before update' actions ? At any point only one of these actions will be executed. Please clarify on the problem here – Dave Jun 3 '15 at 14:05
  • 3
    Please post your code to help people to help you – Vigneshwaran G Jun 3 '15 at 14:14
  • Neha, i've reopend the question, but it would be good if you update the explanation on what's going wrong with some more detail. – Samuel De Rycke Jun 4 '15 at 12:12
  • 1) there is no Trigger.inUpdate it is Trigger.isUpdate; 2) The first if (Trigger.isBefore) is superfluous; 3) updating related records should be done in an after insert trigger; 4) There are many recipes on how to control trigger recursion using static variables – cropredy Jun 4 '15 at 16:02
1

When an update is happening, both the before update and after update triggers are executed.

A possible reason for an update happening after an insert could be that there is a field update workflow rule that changed a value on one of the fields when the record was inserted.

1

This trigger should not update objects. "Before update/insert" triggers are executed in same transaction with primary dml event and BEFORE object will be updated. Just remove DML operations and work with Trigger.New list. All changes that you do in this list will automatically be saved when trigger finishes

  • The code does not update any record of trigger.new, instead it updates the record(ob2) in list - lsttest which has been queried in the trigger, and is not a part of trigger.new. This record will need to be updated with DML operation. – Test Jun 4 '15 at 14:32

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.