There are situations when the data fetched from the database isn’t enough and you need to add more information to each record, namely based on the values returned. OutSystems allows you to do it. You can add new attributes to the records returned by the aggregate based on the value of the other attributes:
- In the aggregate, click on New Attribute to add a new attribute to the aggregate and name it;
- Open the attribute menu and select Edit formula...;
- Define the expression to calculate the value.
In the GoOutWeb, a web application to check interesting places, we have a screen to list places. In this screen, we want to allow the end-user to filter the listed places by category selecting an available category from a Combo Box. Additionally, in each entry of the Combo Box we want to display the number of places that have that specific category along to the category name.
To calculate this data and add it to each entry of the Combo Box:
- Since this is a web application, add an aggregate to the screen’s preparation;
- Add the Place entity to it;
- In the aggregate, do the following:
- Group by Category.Id;
- Count by Place.Id to have the number of places per each category;
- Group by Category.Label to get the label of each category;
- Open the last grouped column menu and add a new attribute. Name it “Combo Box Label”;
- Assign the following expression to the created column:
If ( Label <> "", Label + " (" + Count + ")" , "Not categorized" + " (" + Count + ")" )where Label and Count variables are two of the previously grouped columns.
- Go to the screen and add a Combo Box. Set its values to be the returning list of the new aggregate.