Let me explain the situation. We want to be able to create child records based on workflows attached to a parent object. This works fine, I have a before update trigger that creates the child record and inserts it. (The trigger clears the state that caused the creation of the child record, so even if the trigger runs again it won't make another record)

Problem is when you add a rollup summary on the parent that counts children, it fires the trigger again when you add the child record, causing a recursion which salesforce stomps on and errors messily.

I've used Static variables to prevent re-running of the trigger and it doesn't help. Only thing I can think of is to create the child record with an @future method, but I prefer to avoid them when I can.

Any advice is welcome.

  • if you share your code here then it is easy to give solution and can see if there are options to solve without using @future – Bforce May 7 '14 at 15:27
  • how can you be creating child objects in a before insert trigger on the parent? There is no ID field defined for the parent object at this point. Normally, one would use after triggers to insert child records; static variables should nicely prevent the after trigger on parent from re-executing during the RSF. – cropredy May 7 '14 at 16:31
  • You are entirely correct, I mis-spoke that. It's only before update. -- corrected the question. Thank you! – ScriptMonkey May 7 '14 at 16:33
  • Unfortunately since it's still running the trigger twice, salesforce gives me the recursion error before it gets the chance to get to line one which is the line that checks the static variable to see if the trigger is running already. – ScriptMonkey May 7 '14 at 16:34
  • I would avoid static variables as a guard. This can create weird temporal dependencies that can be difficult to understand and test. – user6861 May 7 '14 at 16:48

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.