NAV Design Pattern of the Week: Single-Record (Setup) Table

NAV Design Pattern of the Week: Single-Record (Setup) Table

  • Comments 3

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[10]. 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.

  

Company-Initialize Codeunit

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.

NAV Usages

Several Setup tables in NAV implement this pattern. Some of those are:

  1. Table 98 General Ledger Setup
  2. Table 311 Sales & Receivables Setup
  3. Table 312 Purchases & Payables Setup
  4. Table 313 Inventory Setup
  5. 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.

 

Best regards

The Reusable Dynamics NAV Patterns team

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
  • 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.

    www.alcim.es

Page 1 of 1 (3 items)