Most times, we need to capture the system date and time of users' actions on NAV. This recipe will illustrate how to get the system date and time.
Let's create a new codeunit from Object Designer.
Now add the following code into the
OnRun
trigger of the codeunit:MESSAGE('Todays Date: %1\Current Time: %2', TODAY, TIME);
To complete the development of the codeunit, save and close it.
On executing the codeunit, you should see a window similar to the one in the following screenshot:
The TODAY
keyword returns the date and the TIME
keyword returns the time from the NAV Server system.
In the case of the older version of the NAV client—specifically the classic client—the date and time are taken from the client computer, which allows users to manipulate the system clock as per their personal requirement.
You can also retrieve the system date and time all at once using the CURRENTDATETIME
function. The date and time can be extracted using the
DT2DATE
and
DT2TIME
functions respectively.
The change log is a base NAV module that allows you to track changes to specific fields in tables. The following code can be found in the 423
, Change Log Management
codeunit in the
InsertLogEntry()
method:
ChangeLogEntry.INIT; ChangeLogEntry."Date and Time" := CURRENTDATETIME; ChangeLogEntry.Time := DT2TIME(ChangeLogEntry."Date and Time");
Here, instead of using the
WORKDATE
function, we use the CURRENTDATETIME
function and then extract the time using the
DT2TIME
function. The system designers can just do the following setup:
ChangeLogEntry.Date := TODAY; ChangeLogEntry.Time := TIME;
The advantage of using CURRENTDATETIME
over TODAY
and TIME
is minimal. CURRENTDATETIME
makes one request to the system while the second method makes two. It is possible that another operation or thread on the client machine could take over between retrieving the date and time from the computer; however, this is very unlikely. The operations could also take place right before and after midnight, generating some very strange data. The requirements for your modification will determine which method is best suited, but generally CURRENTDATETIME
is the correct method to use.