Saturday 19 October 2013

Autocommit in microflows

At my first post I discussed a custom constrained by, by using a Microflow as a datasource. In the example I used an Order management and created a new "Orderline". I want to go on this example and discuss how Mendix works with a phenomenon "Autocommit". 

I'm going to save the new "OrderLine" with a Microflow trigger on the Form "OrderLine_NewEdit". Because it is a Microflow, it will not trigger the standard textbox validations. I have to build the validation myself. When the "OrderLine" is valid, I retrieve the "Order" from the association and calculate the total order price. Then the "OrderLine" is committed and the Form is closed.
When I debug the Microflow and look at the state of each object before and after committing the "OrderLine", something occurs. In the first step the "Order" as the "OrderLine" has the state "instantiated". The second step the "OrderLine" is committed to the database and gets the state "normal". The "Order" however has the state "autocommitted".


When the "OrderLine" is committed to the database, the association to the "Order" is also committed. In the database it isn't possible to refer to an object that doesn't exist. So Mendix inserts the "Order" and gives it the state "autocommitted". Based on this Mendix knows that cancelling the object must lead to a delete in the database, because it was't committed explicit by any logic.
  • Instantiated: A new object that is only present in the application memory.
  • Normal: An object that is present in the database.
  • Autocommitted: An object inserted in the database by an association.

Saturday 12 October 2013

Reference selector with special constrainted by

My first post:
We all have sometimes the situation that we have to commit an object, because the retrieval of data for an reference selector will not work. The data depends on the selection of something else but the selection isn't of the same object. For example we have a form for an "Orderline" what contains the reference selector for the "Product". Here comes the tricky part, it may only be that "Product" that belongs to a "Category" selected in the "Order". The domain model can looks likes this.
In the modeler you can select the "Constraint by" property on a reference selector to let Mendix automatic constraint the data based on the associations. But in this case the "Order" is a new object and not committed to the database, so the results for the "Product" reference selector is empty. 



The reason for the missing data has to do how the constraint by function works. The constraint by function works for the first association, in this case "ODM.OrderLine_Order", and the other associations needs to be in the database, except the last one. The association "ODM.Order_Category" was not in the database and therefor the results were empty.

To solve this issue isn't to be committing the "Order" object before adding a new "OrderLine", but to use a datasource Microflow for the Reference Selector. In that Microflow you can retrieve the "Order" over association (from the memory of Mendix). Than retrieve all products that are attached to category based on the association of "ODM.Order_Category". You just don't have to retrieve the "Category" object first.