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

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


Sometimes it is necessary to retrieve only a part of a date. NAV has built-in functions to do just that. We will show you how to use it in this recipe.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    Day

    Integer

    Month

    Integer

    Year

    Integer

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

    Day := Date2DMY(TODAY, 1);
    Month := Date2DMY(TODAY, 2);
    Year := Date2DMY(TODAY, 3);
    MESSAGE('Day: %1\Month: %2\Year: %3', Day, Month, Year);
    
  4. Save and close the codeunit.

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

How it works...

The Date2DMY function is a basic feature of NAV. The first parameter is a date variable. This parameter can be retrieved from the system using TODAY or WORKDATE, a hard-coded date such as 01312010D, or a field from a table such as Sales Header or Order Date.

The second parameter is an integer that tells the function which part of the date to return. This number can be 1, 2, or 3 and corresponds to the day, month, and year (DMY) respectively.

There's more...

NAV has a similar function called Date2DWY. It will return the week of the year instead of the month if 2 is passed as the second parameter.

Determining depreciation

Codeunit 5616, Depreciation Calculation, contains functions to calculate depreciation based on start and end dates. In order to correctly calculate these values, you must know some details such as the number of days between two dates and whether or not any of those days is a leap day. It is with these types of operations that date functions like DATE2DMY are extremely useful. Have a look at the function DeprDays365 in this codeunit.

StartingYear := DATE2DMY(StartingDate,3);
EndingYear := DATE2DMY(EndingDate,3);
LeapDays := 0;
IF (DATE2DMY(StartingDate,1) = 29) AND (DATE2DMY(StartingDate,2) = 2) AND (DATE2DMY(EndingDate,1) = 29) AND (DATE2DMY(EndingDate,2) = 2) THEN LeapDays := -1;
ActualYear := StartingYear;
WHILE ActualYear <= EndingYear DO BEGIN LeapDate := (DMY2DATE(28,2,ActualYear) + 1);
IF DATE2DMY(LeapDate,1) = 29 THEN BEGIN
IF (LeapDate >= StartingDate) AND (LeapDate <= EndingDate) THEN
LeapDays := LeapDays + 1;
END;
ActualYear := ActualYear + 1;
END;
EXIT((EndingDate - StartingDate) + 1 - LeapDays);

See also

  • Retrieving the system date and time

  • Retrieving the work date

  • Converting a value to a formatted string

  • Repeating code using a loop

  • Checking for conditions using an IF statement