XPATH count constraint?

0
Company associated with Employees (1-N). I want to show only the companies having more than 5 employees in a datagrid. What's the XPath syntax? This won't work: [count(MyFirstModule.Employee_Company/MyFirstModule.Employee) >= 5]
asked
3 answers
2

Since you can't use microflow attributes in XPaths, you somehow need to have an attribute to filter on in the database. Probably the best is to do this using event handlers on the Employee entity.

Something which could work: Add an attribute, let's say NumberOfEmployees to the Company entity.

Then, either create or add to the following existing event handlers:

  • In the After Delete microflow on Employee, do a database retrieve on all the employees which belong to the same company as the employee which just got deleted. Use an aggregate list count on the resulting list and write the resulting integer to the NumberOfEmployees attribute of the company. (and commit)

  • In the Before Commit flow, retrieve the database version of the object and compare if:

    • The company reference has changed since the last commit to the database.
    • Or if the database version is empty (meaning the object is new.

If one of these is true, do the following: Retrieve the Employee objects which belong to the Company the Employee is being assigned to this commit, aggregate the list using count, and add one to this result (since the object being committed is not yet committed as belonging to the company) and update NumberOfEmployees. If the employee belongs to a company in the database entry, do the same as above for the previous company, but make sure to exclude the Employee currently being committed from the retrieve action (id!=$employee).

The above should ensure that if an employee is newly added to a company, switches companies or is deleted, that the employee count for these companies is updated.

answered
0

I think you have to do the counting bit in an aggregate list activity in a microflow, which would then give you a variable, but I'm not sure if you could use that variable in an Xpath constraint.

answered
0

From https://forum.mendix.com/questions/271/max-and-min-etc-with-XPath-queries (Jonathan V.'s answer) I understand that it is not possible to use the functions in an XPath constraint and return Mendix objects (It is only possible to retrieve the aggregate value).

Which leaves workarounds like Sjoerd sugested.

answered