How to set associations via micro flow?

0
I exported my data tables and a list of all the associations between both lists. I made a number of corrections in the association list and now I would like to upload this list back into the app.My current attempt is uploading the 2 columns in an temp entity and use a microflow to set the associations again. But that last part is not working. I have list with the associations in the micro flow, I loop through the list and use a change object to add the association for each record. Afterwards I commit everything.But this does not have any effect. Please advise what to do?
asked
2 answers
1

Hi Renzo,


Your approach is valid. The main thing to check is that you're changing the correct side of the association and that you're working with the actual persistent objects rather than only the temporary import objects.


For example, if your association is:

Parent 1 --- * Child


and your temporary entity contains:

ParentID | ChildID


the microflow could be:

Import Excel/CSV
      ↓
Temporary Association List
      ↓
Create empty Child List
      ↓
Loop through imported rows
      ↓
Retrieve Parent using ParentID
      ↓
Retrieve Child using ChildID
      ↓
Change Child
    Parent_Association = Parent
      ↓
Add Child to Child List
      ↓
End Loop
      ↓
Commit Child List


For a 1- association*, the reference is normally maintained on the many-side, so in this example I would change the Child object and set its Parent association.


Also, I would not commit each object inside the loop. For better performance and to avoid unnecessary database transactions, collect the changed objects in a list and commit the list once after the loop.


So instead of:

Loop
 → Change Object
 → Commit Object


use:

Create List
     ↓
Loop
 → Change Object
 → Add to List
     ↓
Commit List


If the association still isn't being created, I would check the association direction, whether the retrieved Parent/Child objects are actually found, and whether you're changing the persistent Child object rather than the temporary import object.


If you share the domain model showing the association and a screenshot of your Change Object activity, it should be possible to identify exactly where the issue is.


Kindly mark this as the accepted answer if it helps.

answered
0

Hi Renzo,


Yes, you can set associations from a microflow. The important part is to make sure that you are changing the owner of the association and that you are changing the actual business object, not only the temporary import object.


For example, if you have:

Order  --- Order_Customer ---> Customer

and Order is the owner, the microflow should retrieve the required Order and Customer objects and then use Change Object on the Order:

Retrieve Order
Retrieve Customer
        |
        v
Change Order
    Order_Customer = $Customer
        |
        v
Commit Order

For a reference set, you can use the same Change Object activity, but choose Add association and provide the object/list that should be associated. Mendix explicitly supports adding and removing objects from a reference set through Change Object.


The part I would check in your current implementation is the association ownership. In Mendix, the association is stored from its owner side. So if you are changing the non-owner object, changing a member there may not give you the result you expect. Check the arrow/ownership in the domain model first.


Also, make sure that the two columns from your temporary entity are being converted into actual object references. For example:

ImportRow
    ObjectAId
    ObjectBId

I would not try to set the association using the IDs directly. Instead:

Loop ImportRow

    Retrieve ObjectA
        by ObjectAId

    Retrieve ObjectB
        by ObjectBId

    Change ObjectA
        ObjectA_ObjectB = $ObjectB

    Commit ObjectA

If the association is a reference set:

Change ObjectA
    Add ObjectA_ObjectB = $ObjectB

For a reference association, assigning the new object replaces the current reference; for a reference set, use Add association if you want to retain the existing associations.


One more thing: if you're doing the change but checking the result on a page immediately afterwards, make sure the changed object is actually committed. Mendix keeps changes in memory until they are committed, and the association data may therefore not yet be persisted to the database.


So I would troubleshoot your microflow in this order:

  1. Check the association's owner in the domain model.
  2. Retrieve both objects from the IDs in your temporary entity.
  3. Change the object on the owner side of the association.
  4. Use Set for a reference or Add association for a reference set.
  5. Commit the owner object after the association change.
  6. Retrieve the association again and verify that the expected object is returned.

For example, if your association is:

Employee  ---> Department

and Employee owns the association:

Retrieve Employee
Retrieve Department

Change Employee
    Employee_Department = $Department

Commit Employee

Then verify it with:

Retrieve Department
    By association: Employee_Department

Mendix supports retrieving associated objects directly through Retrieve by association, so this is a good way to verify that the relationship was actually created rather than just checking the temporary import data.


If you share the two entities, the association name/type (Reference or Reference Set), and which side owns the association, it should be possible to pinpoint why the current Change Object isn't persisting the relationship.

answered