0

I have a trigger that runs on before insert and before update using trigger.new. I am sending data in batches, so the tirgger.new holds multiple records, some inserts, some updates. I also have some processes and flows that update these records as they are inserted/updated. I only want the trigger to run once for each inserted record and run once for each updated record.

What is happening now is that I insert the record, then the process gets called, so the update is run and then all my subsequent records that need to be don't run through trigger because I do a recursive check to make sure there is only 1 run. I could take that out, but then I am going to get multiple runs for each record.

Is there some condition or some way to flag my records that need to be inserts vs the ones that need to be updated?

  • I assume you are therefore doing an "upsert"? AFAIK the objects are segregated and the trigger called multiple times with only like-event objects of the trigger's object type (note that upsert can only be used for objects of the same type, not a heterogeneous list). – Phil W Apr 29 '19 at 17:06
  • @PhilW yes its an upsert on opportunity objects. Some will be inserts, some will be updates. – MarkC5000 Apr 29 '19 at 17:24
  • You can use a static boolean variable in your class and set it to true when the trigger is run the first time, then the next time the trigger is run, you just need to do a return if the variable is true. – Kevan Apr 29 '19 at 18:06
  • @Kevan yes that's what I want to do, but I have multiple process builders being run, so they run multiple times for all the inserts and my update batch comes later. So I want to make sure those records that are only updates run through the trigger. Right now its only the inserts. – MarkC5000 Apr 29 '19 at 19:23
  • You should consider eliminating the process builders/flows and consolidating all the functionality into your trigger handlers. It will be easier to control and manage. – David Cheng Apr 29 '19 at 19:38

Your Answer

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

Browse other questions tagged or ask your own question.