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

Creating an Option variable


If you need to force the user to select a value from a pre-defined list then an Option is the way to go. This recipe explains how to create an Option variable and access each of its values.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    ColorOption

    Option

  3. Set the following property on the variable:

    Property

    Value

    OptionString

    None,Red,Green,Blue

  4. Add the following code to the OnRun trigger of your codeunit:

    ColorOption := ColorOption::Red;
    CASE ColorOption OF
    ColorOption::None: MESSAGE('No Color Selected');
    ColorOption::Red: MESSAGE('Red');
    ColorOption::Green: MESSAGE('Green');
    ColorOption::Blue: MESSAGE('Blue');
    END;
    
  5. When you run the codeunit you should see a window similar to the following screenshot:

How it works...

An Option is a field or variable that stores one value from a selectable list. In a form, this list will appear as a drop-down from which the user can select a value. The list of options is stored as a comma-separated string in the OptionString property.

These values are accessed using the variable_name::option_name syntax. The first line of the example assigns one of the possible values (Red) to the variable. Then we use a CASE statement to determine which of the values was selected.

There's more...

You can also access possible options in other ways. In a database, an Option is stored as an integer. Each Option corresponds to a specific number, starting with the number 1. In this case None=1, Red=2, Green=3, and Blue=4. You could write this code to perform the safe actions:

ColorOption := ColorOption::"1";
option variableworkingCASE ColorOption OF
ColorOption::None: MESSAGE('No Color Selected');
ColorOption::Red: MESSAGE('Red');
ColorOption::Green: MESSAGE('Green');
ColorOption::Blue: MESSAGE('Blue');
END;

To reduce your development time, you can also use a shorthand notation to access the Option values. Again, the following code is exactly the same as that above:

ColorOption := ColorOption::R;
CASE ColorOption OF
ColorOption::None: MESSAGE('No Color Selected');
ColorOption::Red: MESSAGE('Red');
ColorOption::Green: MESSAGE('Green');
ColorOption::Blue: MESSAGE('Blue');
END;

When you close, save, and reopen the codeunit, the Option values will automatically be filled in for you. That is, both of these examples will look exactly like the first example once it has been saved and reopened. It is always best to write the code exactly as you want it to appear.

Using Options in documents

Option fields are prevalent throughout the NAV system, but most commonly on documents. In NAV, many documents share the same table. For example, sales quotes, orders, invoices, and return orders are all based on the Sales Header table. In order to distinguish between the types, there is an Option field called Document Type. Design table 36, Sales Header, to see the available options for this field.

Now, design codeunit 80, Sales-Post. Examine the OnRun trigger. Early in the function, you will see the following code:

CASE "Document Type" OF
"Document Type"::Order:
Receive := FALSE;
"Document Type"::Invoice:
BEGIN
Ship := TRUE;
Invoice := TRUE;
Receive := FALSE;
END;
"Document Type"::"Return Order":
Ship := FALSE;
"Document Type"::"Credit Memo":
BEGIN
Ship := FALSE;
Invoice := TRUE;
Receive := TRUE;
END;
END;

This is a common example of how Options are used in NAV. You can scroll through the codeunit to find more examples.

See also

  • Using a CASE statement to test multiple conditions