Use the Blocked field and the Blocked Entity Design Pattern

Completed

Master records are used with transactions, which means that after users have worked with the application for a while, many transactional records will relate to a master record. Many master records become obsolete at a specific time. For example, a customer might stop being a customer, you might stop purchasing goods from a vendor, or an item might be discontinued. In these situations, you typically don't want to delete master records but rather block them so that they remain available for analysis or comparison purposes. However, you can no longer use them in transactions. The field that controls this functionality is called Blocked (type Boolean) and it is present in all master tables.

Occasionally, the Blocked field is an Option type field that enables several types of blocks, letting the master record be used in one set of transactions and preventing it from being used in other transaction types. For example, the Blocked field in the Customer and Vendor tables is of type Option.

Not every table that contains the Blocked field is a master table. However, most of the tables that use this field will use it in a similar manner. You should never write logic that handles the Blocked field directly in the master table. However, you should make sure that you check the Blocked field in the code of any tables that refer to or use the master table. For example, you should check whether an item is blocked in the OnValidate trigger of the No. field in the Sales Line table to make sure that people can't use a blocked item in a sales transaction.

Blocked Entity Design Pattern

The Blocked Entity Design Pattern describes the usage of a Blocked field. This pattern is used when it is required to temporarily or permanently stop transactions for a specific entity (mostly master data).

To implement the Blocked Entity Design Pattern, you can create a Blocked field and a function for each transaction in the entity table. This function will check the state of the Blocked field and throw an error if you can't use this specific entity. The function is called from the related transaction.

The following entity relationship diagram shows where an IsBlocked function is defined in the entity table. This function will be called in the transaction (in the DoTransaction function).

Entity Relationship Diagram Blocked Entity Pattern

The next code snippet shows how you can use the Blocked field with an Option data type. The Customer has two functions, CheckBlockedCustOnDocs and CheckBlockedCustOnJnls, that are called during the transaction processing on documents and journals.

table 18 Customer
{
    fields
    {
field(1; "No."; Code[20])
        {
        }
        field(2; Name; Text[100])
        {
        }
        ...
        field(39; Blocked; Option)
        {
            OptionMembers = " ",Ship,Invoice,All;
        }
        ...
    }

    procedure CheckBlockedCustOnDocs(...)

    procedure CheckBlockedCustOnJnls(...)
}