1

some fields on orderitem can only be changed when the order is changed, so I need orderitem trigger to know if it's triggered by order trigger. how can I do this?

Thanks

3

One way to achieve this is using a static variable. In your Util or other similar class, define a static variable called inOrderTrigger.

public static inOrderTrigger = false;

Then in your order trigger, set it to be true.

Then in your orderitem trigger, you can check the value of that static variable.

  • Thanks for the fast reply. if I change the value to true in my order trigger. how long does it stays true? for the whole transaction or forever? – Lee Oct 4 '16 at 23:24
  • @Lee It stays true in your execution context: trailhead.salesforce.com/en/apex_basics_dotnet/…. Basically, you can understand it as it only stays true for current transaction – Lance Shi Oct 4 '16 at 23:34
  • do I need to reset it back to false at the end of the order item trigger? – Lee Oct 4 '16 at 23:35
  • 1
    @Lee the order trigger should set it back to false to handle cases like recursion, mass updates, etc. Basically: isOrderTrigger = true; update lineItems; isOrderTrigger = false; – sfdcfox Oct 4 '16 at 23:56
  • 1
    @Lee there might be, but this is how I do it, and it works well enough. – sfdcfox Oct 5 '16 at 1:56

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.