Handling MultiEntry unit output
From Wiki
This article discuss how to write a script Groovy in order to filter the output of a multientry unit. The goal is to get arrays containing only valid values coming from the multientry unit in order to use them for operations. This is useful, for example, when you use a dynamic template for the multientry unit. In fact, in this case, the user can fill in only some of the available rows. You have to filter the rows in order to consider only the ones that aren't empty. Let's see a working example. Suppose to have a data model like the following and suppose to model a page for entering new orders.
The WebML model of the page that allows to insert new order can be modeled as follows:
- an entry unit with all the fields necessary to fill in all the information related to the Order entity
- a multientry unit which allows to insert the order lines, having all the fields related to the Order Line entity. In the Properties View type 10 in the Min Length field. The you have to set the Layout for the multientry unit. Choose the Layout tab in the Properties View, having selected the multientry unit in the grid, and choose the "WRDefault/Dynamic" unit template.
- the operation chain, which is composed by:
- the create unit that insert the Order entity data
- the script unit that filters the multientry output
- the create unit (bulk) that insert the Order Line entity data
Finally we have to write the script Groovy that removes the empty rows from the multientry output. Let's see how to do this. First of all the script unit must have as many input as the number of the multientry fields. The number of the output must be the same. This is a sample script.
1: def index = 0;
2:
3: def productList = []
4: def quantityList = []
5:
6: for(product in products){
7:
8: if (product != ""){
9:
10: productList.add(products[index]);
11: quantityList.add(quantities[index]);
12: }
13:
14: index++;
15: }
16:
17: return ["productList": productList.toArray(), "quantityList": quantityList.toArray()]
The script Groovy has to
- create a new empty list for each script output (line 3-4)
- iterate over a script input variable (line 6)
- in each iteration, check whether one of the field is not empty. If there is at least one field that is not empty, the script adds the row in output lists (line 7-15)
- construct the map containing all the script output (line 17)
This is only an example. You can customize the script as you want in order to filter the multientry unit output using other criteria.


