When a transaction is started with a record as a parameter, the transaction sometimes need extra information to decide how the transaction needs to behave. When using the OnRun()
trigger of a Codeunit to start the transaction, we can only use one table as a parameter to the function. The Select pattern helps us in working around this limitation in an elegant way, allowing us to write clean and readable code.
More information about the pattern can be found in the How Do I video at https://www.youtube.com/watch?v=SxywT2XSpcI&list=PLhZ3P-LY7CqmVszuvtJLujFyHpsVN0U_w&index=7.
To be able to give the extra information to the function, we need to add extra fields to the table that acts as placeholders or properties. These fields are not available from the user interface, and should be non-editable.
They need to be set prior to calling the transaction, but they don't have to be written to the database. They are kept in the memory during the transaction. At the beginning of the transactions, the values need to be verified. When the transaction ends, the values need to be reset to the default values.
The Select Pattern can be implemented in tables that are used in transactions to create history; or in any scenario where the OnRun
trigger of a Codeunit is used, and extra parameters need to be passed to the code in the Codeunit.
Alternatively, the Codeunit can have Global variables that can be set via functions prior to calling the Codeunit. Microsoft uses a mix of both solutions. The Select Pattern is an elegant way, since it requires less coding than using functions. An example of a function can be found in Codeunit 80, Sales-Post. This has a function called SetPostingDate
that is called from reports 296, Batch Post Sales Orders and 297, Batch Post Sales Invoices.