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

Creating an option variable


If you need to force the user to select a value from a predefined list, 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. Let's create a new codeunit from Object Designer.

  2. Then add the following global variable:

    Name

    Type

    ColorOption

    Option

  3. With the cursor on the ColorOption variable, navigate to View | Properties or (Shift + F4).

  4. In the Property window, set the following property:

    Property

    Value

    OptionString

    None,Red,Green,Blue

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

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

  6. Save and close the codeunit.

  7. On executing the codeunit, you should see a window similar to the one shown in 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 dropdown from which the user can select a value. The list of options is stored as a comma-separated string in the OptionString property. If we query such stored values from a SQL database, we will receive an integer value representing each option. In our current example, the integer value has been mapped with options, where None = 0, Red = 1, Green = 2, and Blue = 3.

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

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.

There's more...

The 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 the 36, Sales Header table to see the available options for this field.

Now design the 80, Sales-Post codeunit. Examine the OnRun trigger. At the start of 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

  • The Using the CASE statement to test multiple conditions recipe in Chapter 2, General Development