0

I'm trying to build a trigger between Accounts and Orders. I've already built the trigger and made it to work but wanted to confirm my understanding.

Requirement: The Sum of Order Amounts should be reflected on Accounts every time a order is added/deleted.

My obsevation: adding a Order (under a Account) creates an empty Order with 0 in Total Amount. Only when you add Order Product, does the amount get reflected in Orders. But, the after insert/update trigger on Order object is unable to get the value for Order Amount (for the record being inserted/updated). To resolve this, I've created an after insert/update trigger on OrderProduct and I'm now able to access the OrderAmount and the totals are correct. However, the same doesn't work for after deletes/undeletes on OrderProduct. The trigger is not detecting the 'cascaded deleted of the child OrderProduct' when the parent Order is deleted (which I assumed it will). So, I had to write another trigger explicitly to track the delete/undelete actions on Order object to be able to add up the OrderAmounts correctly.

Please confirm if I'm missing something with the way Order is behaving or OrderProduct is behaving. Is there anyway I can solve this just 1 triggers - be it either on Order or OrderProduct object.

  • I've never used Order and OrderProduct (the latter frustratingly has no documentation, as per usual for standard objects), but it appears the TotalAmount is a summary roll-up field, which should mean that whenever an OrderProduct is manipulated, it calls the update trigger for Order. Is that not happening? – IllusiveBrian Apr 11 at 2:45
  • @IllusiveBrian Thank you. It does during Add/Update for OrderProduct. However, when I delete a Order (cascade delete to OrderProduct) and the trigger is on OrderProduct (after delete/undelete), it doesn't pick up the changes. – SFG Apr 11 at 3:27
0

There are certain operations in which trigger doesn't fire. Cascading delete is one of those. Here is the complete list from Salesforce - Operations that doesn't fire trigger

You can have the trigger on Order on 'After Delete' operation and call the logic from the OrderProduct handler to recalculate the values.

  • Thanks for your reply, Anand. The After Delete operation on Order is working as expected. But, when you try to do After Insert/Update on Order, it picks up all the order amount except for the one that's being inserted. That's the issue. Hence, I had to come up with this 2 trigger solution. Let m know your thoughts. – SFG Apr 11 at 7:27
  • Can you paste your code of what you have done so far, we can look into that? – Manish Anand Apr 11 at 8:20

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.