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

Repeating code using a loop


Looping is an essential part of any data manipulation. Same as the other programming languages, C/AL offers a variety of looping methods. The following recipe will help you understand how to use the FOR loop in C/AL code.

How to do it...

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

  2. Then add the following global variables:

    Name

    Type

    n

    Integer

    i

    Integer

    Factorial

    Integer

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

    Factorial := 1;
    n := 4;
    FOR i := 1 TO n DO BEGIN
      Factorial := Factorial * i;
      MESSAGE('Factorial of %1 = %2', n, Factorial);
    END;
  4. To complete the task, save and close the codeunit.

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

How it works...

A FOR loop has four parts: a counter, a starting value, the step to be taken, and an ending value. In this code, our counter variable is i. The starting value is 1 and the ending value is n, which in this case has been assigned...