Class-Method-Property

The Microsoft Dynamics NAV Development Environment allows us to write code in almost any place within the application. Each object has dozens of triggers that allow writing C/AL code.

If we just write bits and pieces of code everywhere in the objects, the maintainability of the application would be hard for anyone, including the original developer.

To write code in such a way that other developers can find their way requires a set of rules. If we apply these rules, it will be easier for others to see what we did, because we will reduce the number of places where we write business logic.

The Table as a class

Before we start coding, we need to find the owner of a process. If we explicitly connect the process to the owner, it will be easier for everyone who works with the object to understand what it can do.

Let's try to clarify this with an example. One of the processes that has a lot of owners in Microsoft Dynamics NAV is printing an address. We can print addresses for customers, vendors, documents, and more. In Chapter 2, Design Patterns, we have learned the Pattern to use and print addresses.

If we want to print a customer address on a report, we have to perform the following steps:

  1. First, create a report that uses the Customer as a dataitem.
  2. Then, declare a global text variable called Addr with a length of 50 characters, and an array length of 8 characters.
  3. Then, we declare Codeunit 365 as a variable.
  4. Finally, we call FormatAddress.Customer(Addr, Customer).

The following screenshot shows the Report Designer window:

The Table as a class

This will return a populated address array that we can use on a report.

But how do we know that the Codeunit FormatAddress exists, and has a function that can be called with a Customer record?

The answer is experience, training, and reverse engineering.

A Codeunit as a method

Wouldn't it be nice if we could look at the Customer table, and see what we can do with it?

To do this, we need to implement a new function on the Customer table. Let's call it FormatAddress. We'll give it the Addr text variable as a parameter. Codeunit 365 can be a local variable in the function, as in the next screenshot:

A Codeunit as a method

When we do this, we can use the FormatAddress function each time we have access to a customer record without declaring a Codeunit, or even knowing that it exists. If we apply logical naming, and group similar functions together, other developers will find it easier to know about these functions. The suggested naming should always contain a verb and noun.

Now that this function exists, we can simply refer to it from our report without declaring the Codeunit. We only need the Text array.

A Codeunit as a method

This way of working with functions as a member of the process owner is normal in object-oriented programming, where we will use classes. Using this methodology, we treat an object in Dynamics NAV as a class. We preferably add business logic to a table, and create a Codeunit if the code becomes complex, which typically is when we need more than 10 lines of code.

Note

This video illustrates the use of this concept including some examples:

https://www.youtube.com/watch?v=d_sGFP40evw

Another example where implementing this methodology makes sense is working with Documents. Most documents have multiple processes such as release, reopen, post, copy, and more. Most of these processes are in Codeunits in the system. By creating these processes as a function in the table, it would be easy for developers to call them from the user interface.

A Codeunit as a method
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset