An action with side effects is not allowed in the microflow. Change Object classed as side effects?

0
In the modeller, I am being shown the error: An action with side effects is not allowed in the microflow because it is used as the source of an attribute. I have determined that the action causing this error is a 'Change Object' attribute. From what I have read it makes sense that any actions that affect the client is not allowed in a microflow which is the source of a calculated attribute, but is it also true that I cannot change the value of a different attribute in this microflow?
asked
2 answers
5

Bryn,

Andrej's answer is correct.  In the microflow that is used for a calculated attribute, you shouldn't change any attributes or make any changes that impact the user interface (even if the Modeler allowed you to, these are not things you would want to do in this microflow).  In order to understand why, a bit of background info may be helpful. 

Calculated attributes are (re)calculated every time an object that contains a calculated attribute is retrieved from the database.  As an example, lets say you have the following:

  • Order entity with a calculated attributed call OrderTotalPrice
  • OrderItem entity with a stored attributed called ItemTotalPrice, where each Order can have zero or more OrderItems
  • The microflow used for the OrderTotalPrice calculated attribute will retrieve all OrderItems for an Order and sum the ItemTotalPrice attribute and return the sum.
  • Lets say your Orders have, on average, 3 items.

If you retrieve 20 Orders on a single page, your calculated attribute will incur the cost to retrieve 60 OrderItems from the database and totaling the Price of each.  If you have multiple users doing retrieves like this, or if you have users retrieving more than 20 Orders, you can see how your app is incurring significant processing cost to calculate totals for Orders that may not have changed since the last calculation took place. 

In this scenario, you would want to calculate the OrderTotalPrice when the value changes, and only when the value changes.  This will minimize your processing cost and result in better performance for your application.  OrderTotalPrice will change when an OrderItem is added or removed from an order, and when the quantity or price of and OrderItem is changed.  Event Handlers on OrderItem will capture all of these situations - I would use After Commit and After Delete event handlers for this, others may choose different event handlers.

Now back to your original question - you would want to change other attributes when changes are made in your app that will impact the value of those attributes.  In the scenario above, changing other attributes in the calculated attribute microflow would lead to even higher processing costs and more significant impacts on app performance.

All of this is a long way of saying that, in my experience, calculated attributes should be used very carefully in Mendix apps.  In fact, if you find calculated attributes on any of the entities in an app with a large number of objects, chances are good that those should be removed. My rule of thumb is: calculation should happen when, and only when, a change in the data warrants it. 

Hope that helps,

Mike

answered
4

Hi Bryan,

I think microflows that are used as source for virtual attributes are subject to similar constraints as rules, namely:

  • A rule cannot change data in the database; the actions to create, delete, change and rollback objects are not available in rules.
  • A rule cannot perform interaction with the client; the actions to show or close forms, to show messages, send validation feedback and the download files are not available in rules.
  • A rule cannot call web services, generate documents or import XML.

https://docs.mendix.com/refguide5/rules

-Andrej

answered