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 string to another data type


Sometimes, a string representation isn't enough. In order to perform certain actions, you need your data to be in a certain format. For example, we are reading data from a text file, so our entire data is simple text, which needs to be converted into an appropriate data type to use it in NAV. This recipe will show you how to change that data into a format that you can use.

How to do it...

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

  2. Now add the following global variables:

    Name

    Type

    Length

    DateText

    Text

    30

    DateValue

    Date

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

    DateText := '01/10/2012';
    EVALUATE(DateValue, DateText);
    MESSAGE('Microsoft Dynamics NAV 2013 launch date is %1', DateValue);
  4. To complete the development, save and close the codeunit.

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

How it works...

The EVALUATE() function takes in two parameters. The first is a variable of the type that we want our value to be converted into. This could be date, time, Boolean, integer, or any other simple data type. This parameter is passed by reference, meaning that the result of the function is stored in that variable. There is no need to do a manual assignment to get a return value.

The second parameter is the string that you need to convert. This text is usually stored in a field or variable, but can also be hardcoded.

Note

EVALUATE() returns a Boolean value when executed. If the conversion is successful, it returns TRUE or 1; otherwise, it returns FALSE or 0. If the function returns FALSE, an error will be generated.

There's more...

The EVALUATE() function is widely used in NAV C/AL code. The following code snippet is taken from the CheckCreditCardData() function of the 825, Do Payment Mgt codeunit:

EVALUATE (IntValue1,FORMAT(TODAY,0,'<Year>'));
EVALUATE (IntValue2,COPYSTR(DOPaymentCreditCard."Expiry Date",3,2));
IF IntValue1 > IntValue2 THEN
  ERROR (Text006, CreditCardNo, DOPaymentCreditCard.FIELDCAPTION ("No."));

Before completing a transaction, the credit card's validity period needs to be checked. The preceding code extracts the year from the current date provided by the TODAY function and the expiry date of the credit card. Both the values are evaluated using the relational operator. If the card has expired, the system will execute a predefined error message in text constant Text006.

See also

  • Converting a value to a formatted string

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

  • The Passing parameters by reference recipe in Chapter 2, General Development