Automatically set attributes before create object

0
I have an object that needs to contain multiple other objects. I want to create the “child” objects from a data grid in the Edit page of the “parent” object and I want to set an auto-increment number for each child of the parent. I don’t want it to be calculated each time, as it will be like a local ID set only once while creating the object.  I’ve tried to do it with a “After Create” event by counting the associated children of the parent and setting the ID to be equal to the counter, but this always returns 0, as it cannot “see” the previously created objects. At the same time, by choosing the ID to be a calculated value, the counter works properly, but this approach changes the values by recalculating the counter each time a new object is created. Does anyone suspect why my first approach isn’t working and how to fix it? It seems a rather simple task to do, though Mendix doesn’t seem to feel the same way.. 
asked
3 answers
0

Suggestion: Give the parent an attribute ‘child-counter’ and the child entity an aftercreate-microflow which increases the ‘child-counter’ by one and storing that value also in an attribute in the child, commiting it ‘without events’  to prevent triggering the ACr again (and again).

answered
0

Hello Manolis,

Are you by any chance retrieving your parent object via XPath? 

Retrieving via association should work even on uncommitted objects but xpaths won’t until you commit that object.

Hope this helps

answered
0

Option 1:

create an attribute of type autonumber, this will make sure each child object gets an unique number higher then the previous one. NOTE : if you create an object, but never commit the object the number is lost. Gaps between the numbers will exist.

 

Option 2: 

Create an attribute of type integer/long, default value is empty for the counter on the child object. Create a custom save button which checks the counter, if empty it must determine the correct number by retrieving the parent by association and retrieve all childs by association and filter the ones with a counter > 0. Sort the list descending based upon the counter, use the head option to retrieve the last one and increment that number with 1.

 

answered