Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Microsoft Dynamics NAV 7 Programming Cookbook
Microsoft Dynamics NAV 7 Programming Cookbook

Microsoft Dynamics NAV 7 Programming Cookbook: Learn to customize, integrate and administer NAV 7 using practical, hands-on recipes , Second Edition

eBook
₱1742.99 ₱2490.99
Paperback
₱3112.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Microsoft Dynamics NAV 7 Programming Cookbook

Chapter 2. General Development

In this chapter, we will learn the following:

  • Displaying the progress bar and data in process

  • Repeating code using a loop

  • Checking for conditions using an IF statement

  • Using the CASE statement to test multiple conditions

  • Rounding decimal values

  • Creating functions

  • Passing parameters by reference

  • Referencing dynamic tables and fields

  • Using recursion

Introduction


C/AL (Client/server Application Language) is a programming language used in Client/server Integrated Development Environment (C/SIDE). Using C/AL, we can create business rules to ensure that the data stored in the database is consistent and meaningful. The main purpose of using C/AL is to manipulate data. Besides handling data, C/AL helps to manage execution of C/SIDE objects (such as a table, page, report, codeunit, query, and XMLport).

This chapter consists of recipes that will make understanding of C/AL very easy.

Displaying the progress bar and data in process


During the execution of a big batch job or reports, if the system is not displaying any progress information it can be frustrating and confusing. To avoid this for our customers, we should always display the progress bar and/or data in progress.

How to do it...

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

  2. Add the following global variables:

    Name

    Type

    ProgressBar

    Dialog

    AmountProcessed

    Integer

    AmountToProcess

    Integer

    PercentCompleted

    Integer

  3. Now let's add the following code to the OnRun trigger of the codeunit:

    AmountToProcess := 500000;
    ProgressBar.OPEN('@1@@@@@@@@@@@\#2############');
    REPEAT
      AmountProcessed += 1;
      PercentComplete := ROUND(AmountProcessed / AmountToProcess *10000, 1);
      ProgressBar.UPDATE(1, PercentComplete);
      ProgressBar.UPDATE(2, PercentComplete);
    
    UNTIL AmountProcessed = AmountToProcess;
  4. Save and close the codeunit.

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

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...

Checking for conditions using an IF statement


Sometimes, we want to execute a section of code on a specific condition; this recipe will help to explain the syntax for the same.

How to do it...

  1. Let's create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    SubType

    SalesHeader

    Record

    Sales header

    RecordsProcessed

    Integer

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

    IF SalesHeader.FINDSET THEN BEGIN
      REPEAT
        RecordsProcessed += 1;
      UNTIL SalesHeader.NEXT = 0;
      MESSAGE('Processed %1 records.', RecordsProcessed);
    END ELSE
      MESSAGE('No records to process.');
  4. Save and close the codeunit to complete the task.

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

How it works...

In order to execute the code that processes the records, there must be records in the table. That's exactly what the first line of the previous code does. It tells the code that if you find some records, then it should do...

Using the CASE statement to test multiple conditions


When we have more than two conditions to test, it will be beneficial to use a CASE statement for better code readability.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Let's add the following global variables:

    Name

    Type

    i

    Integer

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

    i := 2;
    CASE i OF
      1:
        MESSAGE('Your number is %1.', i);
      2:
        MESSAGE('Your number is %1.', i);
      ELSE
        MESSAGE('Your number is not 1 or 2.');
    END;
  4. It's time to 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 CASE statement compares the value given, in this case i, to various conditions contained within that statement. Each condition other than the default ELSE condition is followed by a colon. The same logic can be written using the IF statement:

IF i = 1 THEN
  MESSAGE('Your number is %1.', i)
ELSE IF i = 2 THEN
  MESSAGE('Your number...

Rounding decimal values


As Navision is a financial system, it's obvious that most of the time we need to handle decimal values, and rounding decimals is a very important part of it. When we are converting high-value currency to low-value currency, a small decimal can make a big difference.

How to do it...

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

  2. Add the following global variables:

    Name

    Type

    AmountToRound

    Decimal

    RoundToNearest

    Decimal

    RoundToUp

    Decimal

    RoundToDown

    Decimal

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

    AmountToRound:=345.8689999999;
    RoundToNearest:=ROUND(AmountToRound,0.01,'=');
    RoundToUp:=ROUND(AmountToRound,0.01,'>');
    RoundToDown:=ROUND(AmountToRound,0.01,'<');
    
    MESSAGE('Amount Before Rounding = %1 \\Rounded to nearest value =      %2'+'\Rounded to upper value = %3\Rounded to lower value = %4',AmountToRound,RoundToNearest,RoundToUp,RoundToDown);
  4. It's time to save and close the codeunit.

  5. On execution of the...

Creating functions


Most programs will need to execute code from different NAV objects. This code is contained in functions. This recipe will show you how to create a function and explain in more detail what functions are.

How to do it...

  1. To start, let's create a new codeunit from Object Designer.

  2. Add a function called CountToN that takes an integer parameter n.

  3. Now add the following global variables:

    Name

    Type

    i

    Integer

  4. Write the following code in the function:

    FOR i := 1 TO n DO
      MESSAGE('%1', i);
  5. Write the following code in the OnRun trigger of the codeunit:

    CountToN(3);
  6. It's time to save and close the codeunit.

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

How it works...

By creating a function, we can reference multiple lines of code using one easy-to-understand name. Our function is called CountToN, and it takes an integer n as a parameter. This function will display a message box for every number ranging between one and the number that is passed...

Passing parameters by reference


Sometimes, we may want our function to modify multiple values. As we can't return more than one value from a function (unless we use an array), it can be beneficial to pass our parameters by reference to the function.

How to do it...

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

  2. Add the following global variables:

    Name

    Type

    SubType

    Length

    CustomerRec

    Record

    Customer

     

    OldName

    Text

     

    50

    NewName

    Text

     

    50

  3. Then add a function called ChangeCustomerName.

  4. The function should take the following parameter:

    Name

    Type

    SubType

    Customer

    Rec

    Customer

  5. Let's write the following code in the ChangeCustomerName function:

    Customer.Name := 'Changed Name'; 
  6. Add another function called ChangeCustomerNameRef.

  7. The function should take the following parameter:

    Name

    Type

    SubType

    Customer

    Rec

    Customer

  8. Place a check mark in the Var column for the parameter.

  9. Write the following code in the ChangeCustomerNameRef function:

    Customer.Name := 'Changed...

Referencing dynamic tables and fields


On occasions, we may need to retrieve data from the system, but not know in advance where that data should come from. NAV accommodates this by allowing you to reference tables and fields dynamically.

How to do it...

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

  2. Add a global function, GetFirstRecord:

  3. The function should take the following parameter:

    Name

    Type

    TableNo

    Integer

  4. Now add the following local variables:

    Name

    Type

    RecRef

    RecordRef

    FieldRef

    FieldRef

  5. With the cursor on the FieldRef variable, navigate to View | Properties or press Shift + F4.

  6. Let's set the following property:

    Property

    Value

    Dimensions

    2

  7. Write the following code in the GetFirstRecord function:

    RecRef.OPEN(TableNo);
    IF RecRef.FINDFIRST THEN BEGIN
      IF RecRef.FIELDEXIST(1) THEN
        FieldRef[1] := RecRef.FIELDINDEX(1);
      
      IF RecRef.FIELDEXIST(2) THEN
        FieldRef[2] := RecRef.FIELDINDEX(2);
      
      IF FieldRef[1].ACTIVE AND FieldRef[2].ACTIVE THEN
     ...

Using recursion


Recursion is not used often in NAV, but the option is available, and can shorten your code. Recursion is the process by which a function calls itself.

How to do it...

  1. Let's create a new codeunit from Object Designer.

  2. Then add a global function called Fibonacci that returns an integer with no name.

  3. Provide the following parameters for the function:

    Name

    Type

    i

    Integer

  4. Now write the following code to the Fibonacci function:

    IF (i <= 2) THEN
      EXIT(1);
    
    EXIT ( Fibonacci(i-1) + Fibonacci(i-2) );
  5. Write the following code in the OnRun trigger of the codeunit:

    MESSAGE('Fibonacci(%1) = %2', 4, Fibonacci(4));
  6. It's time to save and close the codeunit.

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

How it works...

The Fibonacci sequence is a series of numbers, where the value in a certain position is the sum of the number in the previous two positions, that is, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on.

A recursive function has two parts. The...

Left arrow icon Right arrow icon

Key benefits

  • Integrate NAV with external applications, using the C/AL or SQL server
  • Develop .NET code to extend NAV programming possibilities
  • Administer the Microsoft NAV 7 server and database

Description

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.

Who is this book for?

If you are a junior / entry-level NAV developer then the first half of the book is designed primarily for you. You may or may not have any experience programming. It focuses on the basics of NAV programming. If you are a mid-level NAV developer, you will find these chapters explain how to think outside of the NAV box when building solutions. There are also recipes that senior developers will find useful.

What you will learn

  • Build tables and perform complex actions on their data
  • Design different types of pages to display and interact with business data
  • Create reports to present information from multiple areas of the system
  • Build solutions that work with the entire Microsoft Office suite of products
  • Learn to work with SQL Server and execute basic queries against the NAV database
  • Diagnose and resolve code problems

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 24, 2013
Length: 312 pages
Edition : 2nd
Language : English
ISBN-13 : 9781849689113
Vendor :
Microsoft
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 24, 2013
Length: 312 pages
Edition : 2nd
Language : English
ISBN-13 : 9781849689113
Vendor :
Microsoft
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 9,236.97
Microsoft Dynamics NAV 7 Programming Cookbook
₱3112.99
Programming Microsoft Dynamics NAV 2013
₱3622.99
Getting Started with Dynamics NAV 2013 Application Development
₱2500.99
Total 9,236.97 Stars icon

Table of Contents

12 Chapters
String, Dates, and Other Data Types Chevron down icon Chevron up icon
General Development Chevron down icon Chevron up icon
Working with Tables, Records, and Queries Chevron down icon Chevron up icon
Designing Pages Chevron down icon Chevron up icon
Report Design Chevron down icon Chevron up icon
Diagnosing Code Problems Chevron down icon Chevron up icon
Roles and Security Chevron down icon Chevron up icon
Leveraging Microsoft Office Chevron down icon Chevron up icon
OS Interaction Chevron down icon Chevron up icon
Integration Chevron down icon Chevron up icon
Working with the SQL Server Chevron down icon Chevron up icon
NAV Server Administration Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(8 Ratings)
5 star 50%
4 star 37.5%
3 star 0%
2 star 0%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




James Liang Nov 13, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book very useful for a new programer that they want to use C/AL to create/Modify Navision.Author use "How to do it" example then use "How It works" to explain how to use this functionand "There's more.." to show more example in Navision.
Amazon Verified review Amazon
Matthias Günther Feb 14, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
GUT
Amazon Verified review Amazon
Jón Heiðar Pálsson Jan 02, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book - easy to use and good examples.This book has helped programmers in Dynamics NAV. Keep on the good work.Jón Heiðar
Amazon Verified review Amazon
Baragiola Diego Oct 30, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have been working with NAV during the last 10 years and I thought to know everything about NAV development, but I was wrong. I find this guide suitable for beginners as well as old consultants like me. The paths explained are very clear, with useful "How to do it..." sections. A must, to keep your knowledge up-to-date.
Amazon Verified review Amazon
Georgios T. Nov 05, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is a great Book, helpful not only to programmers but also to any functional Consultant. It is analytical and very clear.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.