Iterating through attributes

0
Hi all,   Is there any trick that allow us to iterate over the attributes of an object?
asked
3 answers
3

Hey Andreia,

Yes, it is possible, but it is not a common thing to do.

1. You can create a Java action to do this. Here you have an example code that you can modify as you wish.

IMetaObject metaObject = mendixObject.getMetaObject();
        
        for (IMetaPrimitive metaPrimitive : metaObject.getMetaPrimitives()) {
            String attributeName = metaPrimitive.getName();
            PrimitiveType attributeType = metaPrimitive.getType();
            
            Object attributeValue = mendixObject.getValue(context, attributeName);
            
            System.out.println("Attribute Name: " + attributeName);
            System.out.println("Attribute Type: " + attributeType);
            System.out.println("Attribute Value: " + attributeValue);
        }

 

If is the first time that you are using/creating java actions, check the official documentation:

https://docs.mendix.com/refguide/extending-your-application-with-custom-java/

 

2. The module MxModelReflection can be also used. Create a microflow that does:

  • Retrieve the MxObjectType object corresponding to the entity you want to iterate.
  • Configure the query to fetch the specific entity by name.
  • Retrieve the list of associated MxObjectMember
  • Add an iterate activity (loop) over the members (MxObjectMember)
  • Inside the loop, you can access the name and type of each attribute.

 

MxModelReflection official doc: https://docs.mendix.com/appstore/modules/model-reflection/

 

Best Regards,

Ricardo Pereira

 

 

answered
3

Sure. Indeed with MxModelReflection like Ganesh said. Here is a sample microflow:

afbeelding.png

And if you like a live example, again, like Ganesh said :-), take a look at microflow MxModelReflection.IVK_RecalculateSize which does this and loops over each attribute, making an estimation of the size per attribute.

answered
2

Yes you can do that in

1. a java action. Pass your mendix object as a param to the java action. Since an entity is a Proxy class in java you can easily access the attributes via the getter and setter methods

2. i think using model reflection you can traverse the attributes within mendix. (you may have to explore this slightly as this might already be done in the MxModelReflections internal microflows

answered