There will be many occasions when you will need to display information in a certain way or multiple variable types on a single line. The FORMAT
function will help you change almost any data type into a string that can be manipulated in any way you see fit.
Let's get started by creating a new codeunit from Object Designer.
Then add the following global variable:
Name
Type
Length
FormattedDate
Text
30
Now write the following code into the
OnRun
trigger of the codeunit:FormattedDate := FORMAT(TODAY, 0, '<Month Text> <Day,2>,<Year4>'); MESSAGE('Today is %1', FormattedDate);
To complete the task, save and close the codeunit.
On executing the codeunit, you should see a window similar to the following screenshot:
The FORMAT
function takes one to three parameters. The first parameter is required and can be of almost any type: date, time, integer, decimal, and so on. This parameter is returned as a string.
The second parameter is the length of the string to be returned. The default, zero, means that the entire string will be returned, a positive number tells the function to return a string of exactly that length, and a negative number returns a string not larger than that length.
There are two options for the third, and final, parameter. One is a number, representing a predefined format you want to use for the string, and the other is a literal string. In the example, we used the actual format string. The text contained in the angular brackets (<
>)
will be parsed and replaced with the data in the first parameter.
Note
There are many predefined formats for dates. Run a search for Format Property
in the Developer and IT Pro Help option in the Help menu of Microsoft NAV Development Environment or visit the following URL:
http://msdn.microsoft.com/en-us/library/dd301059(v=nav.70).aspx
The following code can be found on the
OnValidate()
trigger of the Starting Date
field from the 50
, Accounting Period
table:
Name := FORMAT("Starting Date",0,Text000);
In the preceding code, Text000
is a text constant and carries the <Month Text>
value. This code will return month of "Starting Date"
in text format.
Retrieving the system date and time
Retrieving the work date
Determining the day, month, and year from a given date
Converting a string to another data type
The Checking for conditions using an IF statement recipe in Chapter 2, General Development
The Advanced filtering recipe in Chapter 3, Working with Tables, Records, and Queries
The Retrieving data using the FIND and GET statements recipe in Chapter 3, Working with Tables, Records, and Queries