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 work date


The work date is an essential part of the NAV system. This recipe will show you how to determine what that date is, as well as when and where you should use it.

Getting ready

  1. Click on Tools | Workdate from the NAV client.

  2. Set the work date to 01/01/2010.

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('Work Date: %1\Todays Date: %2\Current Time: %3', WORKDATE, TODAY, TIME);
    
  3. Save and close the codeunit.

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

How it works...

The work date is a date internal to the NAV system. This date is returned using the WORKDATE keyword. It can be changed at any time by going to Tools | Work Date.

There's more...

It is important to understand the difference between the NAV work date and the computer system date. They should be used in specific circumstances. When performing general work in the system, you should almost always use the WORKDATE keyword. In cases where you need to log information and the exact date or time when an action occurred, you should use TODAY and TIME or CURRENTDATETIME.

Populating date fields when a document is created

The following code can be found in table 36, Sales Header, in the InitRecord() method:

IF "Document Type" IN ["Document Type"::Order,"Document Type"::Invoice,"Document Type"::Quote] THEN BEGIN
methodInitRecord()"Shipment Date" := WORKDATE;
"Order Date" := WORKDATE;
END;
IF "Document Type" = "Document Type"::"Return Order" THEN
"Order Date" := WORKDATE;
IF NOT ("Document Type" IN ["Document Type"::"Blanket Order","Document Type"::Quote]) AND ("Posting Date" = 0D) THEN
"Posting Date" := WORKDATE;
IF SalesSetup."Default Posting Date" = SalesSetup."Default Posting Date"::"No Date" THEN
"Posting Date" := 0D;
"Document Date" := WORKDATE;

It is common to create and call an InitRecord() method from a table's OnInsert trigger especially for document-style tables. Unlike with the InitValue property for fields in a table, fields here are filled in based on conditional logic. More importantly, validation can be performed to ensure data integrity.

Looking at this snippet of code, we can see that every date is filled in using the WORKDATE keyword, and not using TODAY. This is so that a user can easily create records that are pre-dated or post-dated.

See also

  • Retrieving the system date and time

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

  • Converting a value to a formatted string

  • Checking for conditions using an IF statement

  • Using a CASE statement to test multiple conditions