Book Image

Microsoft Dynamics NAV 2009 Programming Cookbook

By : Matthew Traxinger
Book Image

Microsoft Dynamics NAV 2009 Programming Cookbook

By: Matthew Traxinger

Overview of this book

Microsoft Dynamics NAV 2009 is a business management solution that helps simplify and streamline highly specialized business processes such as finance, manufacturing, customer relationship management, supply chains, analytics, and electronic commerce for small and medium-sized enterprises. ERP systems like NAV thus become the center of a company's day-to-day operations. When you learn to program in an environment like this it opens up doors to many other exciting areas like .NET programming, SQL Server, and Web Services.Microsoft Dynamics NAV 2009 Programming Cookbook will take you through interesting topics that span a wide range of areas such as integrating the NAV system with other software applications like Microsoft Office, creating reports to present information from multiple areas of the system, and so on. You will not only learn the basics of NAV programming, but you will also be exposed to the technologies that surround the NAV system such as .NET programming, SQL Server, and Web Services.The first half of the cookbook will help programmers coming to NAV for the first time by walking them through the building blocks of writing code and creating objects like tables, forms, and reports. The second half focuses on using the technologies surrounding NAV to build better solutions. You will learn how to write .NET code that works with the NAV system and how to integrate the system with other software applications like Microsoft Office or even custom programs. You will also discover some of the features of the Role Tailored Client including creating Pages and custom add-ins.
Table of Contents (18 chapters)
Microsoft Dynamics NAV 2009 Programming Cookbook
Credits
About the author
About the reviewer
Preface
Index

Retrieving the system date and time


There are many instances when it is necessary to obtain the current date and time from the user's system. This recipe will show you how to get that information.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Write the following code in the OnRun trigger of the codeunit:

    MESSAGE('Todays Date: %1\Current Time: %2', TODAY, TIME);
    
  3. Save and close the codeunit.

  4. When you run the codeunit you should see a window similar to the following screenshot:

How it works...

The TODAY keyword returns the date from the system clock on the client computer. In Windows, the current system time is usually located at the bottom-right corner of the task bar. The same holds true for the system time which is returned by the TIME keyword.

There's more...

The actual date and time returned depends on which version of the NAV client you are using. In the RoleTailored client, the date and time come from the NAV server. In the Classic client, the date and time come directly from the client computer and users will be able to manipulate the system clock to their advantage if they need to. An example could be a time clock application where a user can clock in, change the system time to eight hours later, clock out, and change it back to the original time.

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.

Note

For a complete list of date functions, search the C/SIDE Reference Guide under the Help menu for date and time functions.

Logging changes and events

The ChangeLog is a base NAV module that allows you to track changes to specific fields in tables. The following code can be found in Codeunit 423, Change Log Management, in the InsertLogEntry() method.

ChangeLogEntry.INIT;
system timeevents, loggingChangeLogEntry."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 could have just done 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 suits best, but generally CURRENTDATETIME is the correct method to use.

See also

  • Retrieving the work date

  • Determining the day, month, and year from a date

  • Converting a value to a formatted string

  • Writing your own rollback routine