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 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. This recipe will show you how to change that data into a format that you can use.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    Length

    DateText

    Text

    30

    DateValue

    Date

     
  3. Write the following code in the OnRun trigger:

    DateText := '01/01/2010';
    EVALUATE(DateValue, DateText);
    MESSAGE('Date: %1', DateValue);
    
  4. Save and close the codeunit.

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

How it works...

The EVALUATE() function takes in two parameters. The first is a variable of the type that you want your 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 assign using the := syntax.

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

For a list of all of the functions related to text variables, search for "Text Data Type" in the C/SIDE Reference Guide under the Help menu.

There's more...

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. If you wish to display the standard system error, you can leave the code as it is, but if you want to handle the error yourself, you must make the following changes:

DateText := '01/01/2010';
IF NOT EVALUATE(DateValue, DateText) THEN
ERROR('Custom Error Message');
MESSAGE('Date: %1', DateValue);

Incrementing a number series

Number series are used throughout the NAV system. Every document has a unique identifier that is usually retrieved from the No. Series table. This table keeps a track of the last number used so that it knows what the next number should be.

However, this identifier is not just a number. A purchase order, for example, might have an identifier of PO123456, which means that it is actually a string. As you can't add a number to a string, you will have to figure out what the number part is, convert it to an actual number, and then increment it. This code from the IncrementNoText() function in codeunit 396, NoSeriesManagement, does exactly that. As this code calls several other functions, it may be beneficial for you to look through the entire codeunit.

GetIntegerPos(No,StartPos,EndPos);
stringnumber series, incrementingEVALUATE(DecimalNo,COPYSTR(No,StartPos,EndPos - StartPos + 1));
NewNo := FORMAT(DecimalNo + IncrementByNo,0,1);
ReplaceNoText(No,NewNo,0,StartPos,EndPos);

See also

  • Converting a value to a formatted string

  • Checking for conditions using an IF statement

  • Passing parameters by reference