Commiting related entities

0
I am setting up a test where I am POSTing and order and order items to an endpoint. I am able to create the order and order items with the following microflow. Is there a better way to do this? I would prefer to commit the order and order items on the same commit such that I save everything or nothing? Thanks for your suggestions.  
asked
1 answers
1

Steve

There are a number of ways to accomplish this.  One that comes to mind:

  • Create OrderOut and Commit
  • Create the OrderItems and add them to a list (create the list before the loop).  
  • After the loop, assuming you haven't encountered anything that would cause you to not commit, commit OrderOut and then the list of OrderItems.  In these commits, use custom error handling and choose Custom with Rollback

This article will give you an overview of error handling and transactions:  https://docs.mendix.com/howto/logic-business-rules/set-up-error-handling

Hope that helps,

Mike

Note: you may wonder why I didn't recommend that you set error handling in the loop while committing each NewOrderItem.  In Mendix, you cannot set custom error handling directly inside of a loop.  So while an error in the loop will have the effect of rolling back all of the objects created in the whole microflow, it won't be very graceful - the user will get a message that says, "An Error Has Occurred".  Committing a list of objects after the microflow enables you to handle an error in a more orderly fashion.  Another way to use custom error handling in a loop is to put the Commit in a microflow which you call in the loop instead of committing directly in the loop.  Which you use is personal preference, I think, unless your list of items is large in which case committing the list of NewOrderItems after the loop has a significant performance advantage over committing each item as it is created.

answered