Business Central – Code vs Text data type

When I first started doing NAV/BC development, I would get a bit confused over the purpose of Code and Text data type from time to time. After all, they are quite similar in function and role. Both of them store a string of text. It is also quite novel to me after working with Java, C++, VB.NET to see this type of data structure.

In the beginning, I assumed that Code data type’s role is for primary key and text is for everything else. While this is a fair assumption, I realized this is not the case as I gained experience over the years. There are use cases where you might want to use a code over text and in others text over code data types.

I have seen people using Code data type for storing record descriptions when a Text data type would be a better option. I have also made similar mistake when I started out, using a Code data type to store password when I should be using a Text data type.

Text data type denotes a text string, they are general purposes and comes with a lot of text manipulation functions. The Text data type is a value type, such that every time you use a method on it, you are creating a new text string object in memory. This means that there will be an allocation of space for each instance you are using it, which equates to overheads. It should not be a deal breaker unless you are dealing with and/or need to manipulate massive amount of textual data.

Code data type denotes a special type of string. The values are always converted and stored in uppercase and all leading and trailing spaces are removed. A Code variable cannot be null. That is what makes it so suitable for use as a primary/unique/referential key data type.