Book Image

Microsoft Dynamics NAV 7 Programming Cookbook - Second Edition

Book Image

Microsoft Dynamics NAV 7 Programming Cookbook - Second Edition

Overview of this book

Microsoft Dynamics NAV 7 is a business management solution that helps simplify and streamline highly specialized business processes. Learning NAV programing in NAV 7 gives you the full inside view of an ERP system. Microsoft Dynamics NAV 7 Programming Cookbook covers topics that span a wide range of areas such as integrating the NAV system with other software applications including Microsoft Office, and creating reports to present information from multiple areas of the system,. We will not only learn the essentials of NAV programming, you will also be exposed to the technologies that surround NAV including.NET programming, SQL Server and NAV system administration. Microsoft Dynamics NAV 7 Programming Cookbook is written in a direct, to-the-point style to help you get what you need and continue working in NAV. The first half of the cookbook will help programmers using NAV for the first time, by walking them through the building blocks of writing code and creating objects such as tables, pages, 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 such as Microsoft Office or even custom programs. You will learn everything you need to know for developing all types of NAV CSIDE objects, as well as how to integrate and maintain a NAV system.
Table of Contents (20 chapters)
Microsoft Dynamics NAV 7 Programming Cookbook
Credits
About the Author
About the Reviewers
Acknowledgements
www.PacktPub.com
Preface
Index

Using the date formula to calculate dates


The date formula allows us to determine a new date based on a reference date. This recipe will show you how to use the built-in CALCDATE NAV function for date calculations.

How to do it...

  1. Let's start by creating a new codeunit from Object Designer.

  2. Add the following global variable by navigating to View | C/AL Globals (Alt + V + B):

    Name

    Type

    CalculatedDate

    Date

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

    CalculatedDate := CALCDATE('CM+1D', 010110D);
    MESSAGE('Calculated Date: %1', CalculatedDate);
  4. Now save and close the codeunit.

  5. On executing the codeunit, you should see a window similar to the following screenshot:

How it works...

The CALCDATE() function takes in two parameters: a calculation formula and a starting date. The calculation formula is a string that tells the function how to calculate the new date. The second parameter tells the function which date it should start with. A new date is returned by this function, so the value must be assigned to a variable.

The following units can be used in the calculation formula:

Unit

Description

D

Day

WD

Weekday

W

Week

M

Month

Q

Quarter

Y

Year

These units may be different depending on what language version NAV is running under.

You have two options to place the number before the unit. It can either be a standard number ranging between 1 and 9 or the letter C, which stands for current. These units can be added and subtracted to determine a new date based on any starting date.

Calculation formulas can become very complex. The best way to fully understand them is to write your own formulas to see the results. Start out with basic formulas such as 1M + 2W - 1D and move on to more complex ones, such as –CY + 2Q - 1W.

There's more...

The following code is part of the CalcNumberOfPeriods() function of the 485, Business Chart Buffer table:

"Period Length"::Week:
  NumberOfPeriods := (CALCDATE('<-CW>',ToDate)- CALCDATE('<CW>',FromDate)) DIV 7;

The preceding code snippet will return the difference between two dates in terms of weeks. <-CW> will provide a week start date of ToDate whereas <CW> will provide a week end day of FromDate. The difference between the calculated days will be divided by 7 to get the total number of weeks.

For more details on CALCDATE, visit the following URL:

http://msdn.microsoft.com/en-us/library/dd301368(v=nav.70).aspx

See also

  • Retrieving the system date and time

  • Retrieving the work date

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

  • The Checking for conditions using an IF statement recipe in Chapter 2, General Development