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

Converting a value to a formatted string


There will be many occasions when you will need to display information in a certain way or 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. Let's get started by creating a new codeunit from Object Designer.

  2. Then add the following global variable:

    Name

    Type

    Length

    FormattedDate

    Text

    30

  3. Now write the following code into the OnRun trigger of the codeunit:

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

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

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. The 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 not 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 the angular brackets (< >) will be parsed and replaced with the data in the first parameter.

Note

There are many predefined formats for dates. Run a search for Format Property in the Developer and IT Pro Help option in the Help menu of Microsoft NAV Development Environment or visit the following URL:

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

There's more...

The following code can be found on the OnValidate() trigger of the Starting Date field from the 50, Accounting Period table:

Name := FORMAT("Starting Date",0,Text000);

In the preceding code, Text000 is a text constant and carries the <Month Text> value. This code will return month of "Starting Date" in text format.

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

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

  • The Advanced filtering recipe in Chapter 3, Working with Tables, Records, and Queries

  • The Retrieving data using the FIND and GET statements recipe in Chapter 3, Working with Tables, Records, and Queries