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.
Trigger.inUpdate
it isTrigger.isUpdate
; 2) The firstif (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