The Reusable Dynamics NAV Patterns is a joint initiative between the NAV team NAV partners. This is an open initiative to anyone who has documented design patterns which are specific to NAV, please reach back to us either by leaving a comment here, or by writing to us. This said, it’s almost weekend, so here you have the NAV design pattern of the week.
Single-Record (Setup) Table
Meet the Pattern
This pattern is intended for storing information about the operating setup or environment in the database, in a way that can be persisted across sessions. To facilitate this, this information is stored in a table with one record only. The user is subsequently able to modify, but not add or delete records in the table.
The most common implementation of this is in the NAV Setup tables.
Use the Pattern
Implementation of the pattern involves 3 considerations:
- Defining a suitable primary key.
- Creating a page where the user can view and edit a record, but not add new records or delete an existing one.
- Optionally, create Company – Initialize codeunit.
Defining a Primary Key
Since this kind of tables is a collection of several environment or setup parameters, the primary key does not refer to any business attributes for this kind of tables. However, for maintaining the integrity of the database, it is necessary to define a primary key.
So, the most common implementation is to have a field “Primary Key” of Code. This is populated with a blank value when the record is inserted. This field is not added to the page, so that the user cannot be modify it later.
Creating a Page
The CardPage type is most suitable for representing this kind of tables. In addition, the InsertAllowed and DeleteAllowed properties in the page should be set to false to prevent the user from adding or deleting records in the table.
In the OnOpenPage trigger, the following code should be added to insert a record when the user opens the page for the first time, if a record does not exist already.
The Company-Initialize codeunit (codeunit 2) is executed when a new company is created. We recommended that you add records to the single-record tables in this codeunit. If some of the fields are expected to have default values, they can also be populated here.
Several Setup tables in NAV implement this pattern. Some of those are:
- Table 98 General Ledger Setup
- Table 311 Sales & Receivables Setup
- Table 312 Purchases & Payables Setup
- Table 313 Inventory Setup
- Table 242 Source Code Setup
Note: While most tables just insert a record with empty primary key in codeunit 2, table 242 (“Source Code Setup”) offers an example of inserting default values into all fields of the table (method “InitSourceCodeSetup”). This practice, wherever feasible, is likely to reduce the effort during implementation.
The Reusable Dynamics NAV Patterns team