Standards, best practices and how-tos for developing OutSystems applications
A computing best practice is not to use literals in your code. This is known as hard-coding and it makes it harder to maintain your code.
The default method to store a list of values in OutSystems is to use a Static Entity. The drawback of using static entities is that each static entity counts for an Application Object (AO). Since most licenses include a limited amount of AO’s consider one of the alternatives to avoid using AO’s for static entities.
This article describes the following alternatives:
For a single value you can just define a local variable and give it a default value. To emphasize its constant behavior you can prefix these local variables with Constant. E.g. ConstantMaxRecords
When a literal is used multiple times you can create a Function Server Action that returns the fixed value. This may be useful for configuration values etc. Also here prefixing the name with Constant is used. E.g. ConstantDefaultPageSize.
Please note: When it’s required to be able to change these values at runtime you should use site properties in O11 or Settings in ODC.
When you require a record list you can import a JSON file and convert it to a structure list.
We provide a JSON with the id and the name of the weekdays.
[
{"id": 1,"name": "Monday"},
{"id": 2,"name": "Tuesday"},
{"id": 3,"name": "Wednesday"},
{"id": 4,"name": "Thursday"},
{"id": 5,"name": "Friday"},
{"id": 6,"name": "Saturday"},
{"id": 7,"name": "Sunday"}
]
Server Action WeekDayGet:
Server Action Function WeekdayGetById:
As an alternative we can import and convert an Excel file.
Given an Excel file Categories with columns Name, Slug and Description we can create a Category structure and CategoryGet and CategoryGetBySlug functions.
Although the solutions above avoid consuming AO’s it add extra complexity to your code. Extra logic is required for the following use cases:
In addition there can be a significant performance impact.
Therefore, make a careful consideration before choosing a static entity or one of the alternatives.