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

Converting a value to a formatted string


There will be many occasions when you will need to display information in a certain way or display multiple variable types on a single line. The FORMAT function will help you change almost any data type into a string that can be manipulated in any way you see fit.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    Length

    FormattedDate

    Text

    30

  3. Add the following code to the OnRun trigger:

    FormattedDate := FORMAT(TODAY, 0, '<Month Text> <Day,2>, <Year4>');
    MESSAGE('Today is %1', FormattedDate);
    
  4. Save and close the codeunit.

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

How it works...

The FORMAT function takes one to three parameters. The first parameter is required and can be of almost any type: date, time, integer, decimal, and so on. This parameter is returned as a string.

The second parameter is the length of the string to be returned. A default zero means that the entire string will be returned. A positive number tells the function to return a string of exactly that length, and a negative number returns a string no larger than that length.

There are two options for the third and final parameter. One is a number, representing a predefined format you want to use for the string and the other is a literal string. In the example, we used the actual format string. The text contained in brackets (< >) will be parsed and replaced with the data in the first parameter.

There's more...

There are many predefined formats for dates. The examples listed in the following table are taken from the C/SIDE Reference Guide in the Help menu of the NAV client. Search for "Format Property" to find more information.

Date

Format

Example

<Closing><Day,2>-<Month,2>-<Year>

0

05-04-03

<Closing><Day,2>-<Month,2>-<Year>

1

05-04-03

<Day,2><Month,2><Year><Closing>D

2

050403D

<Closing><Year>-<Month,2>-<Day,2>

3

03-04-05

<Closing><Day>. <Month Text> <Year4>

4

5. April 2003

<Closing><Day,2><Month,2><Year>

5

050403

<Closing><Year><Month,2><Day,2>

6

030405

<Day,2><Filler Character, >. <Month Text,3> <Year4>

7

5. Apr 2003

XML format

9

2003-04-05

Creating filters using other variable types

You will often need to create filters on dates or other simple data types. Usually these filters are not just for a single value. For example, a date filter for all values between January 1st, 2010 and January 31st, 2010 would look like 010110..013110. Because ".." is a string, and you cannot concatenate it with two date variables. Instead, you will have to convert those dates into strings and then place the filters together.

Take the CreateAccountingDateFilter function from codeunit 358, DateFilter-Calc. It creates date filters based on accounting periods for the exact scenario we are describing.

AccountingPeriod.RESET;
IF FiscalYear THEN
AccountingPeriod.SETRANGE("New Fiscal Year",TRUE);
AccountingPeriod."Starting Date" := Date;
AccountingPeriod.FIND('=<>');
IF AccountingPeriod."Starting Date" > Date THEN
NextStep := NextStep - 1;
IF NextStep <> 0 THEN
IF AccountingPeriod.NEXT(NextStep) <> NextStep THEN BEGIN
IF NextStep < 0 THEN
Filter := '..' + FORMAT( AccountingPeriod."Starting Date" - 1)
ELSE
Filter := FORMAT(AccountingPeriod."Starting Date") + '..' + FORMAT(12319999D);
Name := '...';
EXIT;
END;
StartDate := AccountingPeriod."Starting Date";
IF FiscalYear THEN
Name := STRSUBSTNO(Text000,FORMAT(DATE2DMY(StartDate,3)))
ELSE
Name := AccountingPeriod.Name;
IF AccountingPeriod.NEXT <> 0 THEN
Filter := FORMAT(StartDate) + '..' + FORMAT(AccountingPeriod."Starting Date" - 1)
ELSE BEGIN
Filter := FORMAT(StartDate) + '..' + FORMAT(12319999D);
Name := Name + '...';
END;

See also

  • Retrieving the system date and time

  • Retrieving the work date

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

  • Converting a string to another data type

  • Checking for conditions using an IF statement

  • Using advanced filtering

  • Retrieving data using FIND