Programmatically Accessing Data


We can programmatically access any data in our model, from anywhere in our model with a few lines of groovy. We can easily slice, query, and perform custom calculations from anywhere, including the Console, Rulebases, Data Lists, or Views.

Accessing Data Within the Current Scope

While inside of a Data List, access to any field within the current Data List can be achieved by simply using the field’s Code Name. The code name is a computer friendly adaptation of the field’s name, in which all of the spaces and special characters have been removed. The “Code Name” can be found on any field by entering edit mode on the Data List, then selecting the header dropdown menu for the Name column, and finally, checking the Code Name box under Columns.

Screenshot

Accessing a Data List globally

To access data outside of the scope of our current Data List, we can use the model class in order to access the global scope.

Hint: The best way to learn is to sandbox in the console before trying to implement inside of a model.

To return every item in the Data List, “Sample Data”:

model.sampleDataList

To get the size (number of items) of the Data List:

model.sampleDataList.size()

In these examples, model denotes that we are starting from the Global model. Within the Global model, lives the Sample Data Data List. We access each level of the tree with dot notation.

Functions on fields

Now that we can access our Data Lists programmatically, what can we do with it? Modelshop has a plethora of built-in functions.

Examples:

We can get the sum of a numerical field in our Data List:

model.sampleDataList.sum{it.exampleField}

We can get the average of a numerical field in our Data List:

model.sampleDataList.average{it.exampleField}

We can get the maximum value of a numerical field in our Data List:

model.sampleDataList.max{it.exampleField}

We can get the minimum value of a numerical field in our Data List:

model.sampleDataList.min{it.exampleField}

We can get the count of a field in our Data List:

model.sampleDataList.count{it.exampleField}

We can also introduce logic:

model.sampleDataList.count{it.exampleField > 0}

Collect Function

The collect function in groovy can be used to iterate over a collection (hence, the name collect) and transform each element in the collection. It differs from the functions above, in that it will return collection of items, rather than one item (such as the sum, average, min, max, or count).

Perform a calculation on every item in the field:

model.sampleDataList.collect{it.exampleField * 2}

Or access multiple fields at once:

model.sampleDataList.collect{it.exampleField * it.otherField}

We can then perform other functions on our transformed collection:

model.sampleDataList.collect{it.exampleField * it.otherField}}.average{it}

Queries

We can query our Data Lists as well. This gives the ability to introduce more powerful logic into our Views, Rules, and Custom Expressions.

.find

A find query will search the given field and return the first item that satisfied the condition set in the function.

model.sampleDataList.find{it.exampleField == 2}

This will return the entire item, meaning we can access any field on that item. For example,

model.sampleDataList.find{it.exampleField == 2}.title

Will search the Sample Data List for the first item that has an Example Field value of 2. It will then return the Title field for that item.

.findAll

As it’s name implies, a findAll query is very similar to a find query. However, rather than returning only the first item that satisfies the given condition, a findAll query will return all items that satisfy the condition, in a collection.

model.sampleDataList.findAll{it.exampleField == 2}

This will return a collection of items, meaning, once again, we can access any field on each of those items. For example,

model.sampleDataList.findAll{it.exampleField == 2}.otherField

Will search the Sample Data List for all items that have an Example Field value of 2. It will then return the Other Field for each of those items as a collection.

Just like the collections returned the collect function, additional functions can be called on queries. From our example above,

model.sampleDataList.findAll{it.exampleField == 2}.otherField.average{it}

Will return the average of the collection returned by our findAll query.