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.
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.
Implementation of the pattern involves 3 considerations:
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.
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:
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
I wasn't aware for the best practice regarding adding the setup table to Codeunit 2.
Thank you for this :)
I just would like to say one thing about the pattern series. FINALLY! :D
I have been looking forward to these and the ones that will come. Keep up the good work!
Very useful information to add to our company best practices. Thank you for sharing it.