Get Etity Atttribute Value By Name

0
Hi Everyone,   I have a requirement where I need to get the *value* of an attribute within an entity (I don't need to search for this value) by the *name* of the attribute.    For example, I might have an entity called 'Worker' with the attributes 'Name', 'Age', 'Badge Number' - I can retrieve the worker by e.g. Name from the database but I want to loop through the attributes and select just 'Name' and 'Badge Number' values for a specific task.    I've looked at the MxModelReflection module - and that might be valuable moving forward and am considering writing a Java Action and using reflection there to get the attribute value but I've read that this can have a big impact on performance.    Is there a better way ro do this?   Thanks!
asked
5 answers
0

The way you describe your challenge, a regular solution is: a retrieve, followed by a loop, in which you have the iterator-object containing the attributes.

However, for some reason (performance), you want only 1 or 2 attributes and perform some action.

So then your best bet is to use OQL. This almost always gets you the most specific attributes and in the fastest way. If you have little experience, try https://service.mendixcloud.com/p/OQL to play-around, but your OQL-statement will look something like this:

from YourModule.YourEntity ye
where ye....somestatement...
select ye.Name, ye.BadgeNumber

 

answered
0

A java-action is the best option, take a look at community commons “clone” java-action.

answered
0

Hi David,

You can achieve this using OQL statement.

 

Check this blog

https://medium.com/@mohammad.saqib_1262/oql-series-part-1-introduction-to-object-query-language-in-mendix-213a0ff132a6

answered
0

Thanks Everyone,

 

Apologies - I think I could have been clearer in my initial question. My requirement is to create a Word document based on the attributes of an entity in the Mendix database. Getting the entity and the data is the easy bit. 

 

I'm using Ivo Sturm's Word Template module from here: How I Automated Microsoft Word Templating in Mendix Using The Apache POI API | by Ivo Sturm | Mendix Community | Medium and it works brilliantly (thanks Ivo!) but I want to map the attributes within my entity to the placeholders in the table that the Word Template module creates so that I can change this mapping if the data or entity structure changes.

 

The idea is that the mapping can be done by the user in a list / grid on a template managment page, so the user can say $Entity/attrubutname maps to <placeholder> in the document.

 

So, in my example, I have a placeholder in my document called <worker_name> that I want to map to the 'FirstName' attribute in my entity. When the document is created the Workers first name is extracted from the entity and added to the document. If my user requirements change and I have to map the <worker_name> placeholder to (for example) the 'FullName' attribute I'd like the user to be able to edit the mapping and it pulls the value of 'Fullname' from the entity.

 

I can hard code the mapping, of course, but it seems really inflexible and hard to manage. I can loop through the placeholder list and send the object and attribute name to a Java Action with Model Reflection but that doesn't seem very performant. Ideally, I'd like to be able to do something like <placholderdata> = $Entity/  + 'attribute_name' but I don't know if that's possible.

 

Sorry for the long winded addition - hope that clarifies things a bit!

answered
0

David,

One idea is to use the Java action GetOriginalValueAsString from Community Commons.  This Java action will retrieve the last committed value for a combination of Entity(Object) and Attribute name.  So if you have the object, and you can get the attribute names from Model Reflection, you can retrieve the value of that attribute on a specific object.

 

I may be missing what you are trying to accomplish, but it seems this might work?

 

Mike

answered