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 2009 Programming Cookbook
Microsoft Dynamics NAV 2009 Programming Cookbook

Microsoft Dynamics NAV 2009 Programming Cookbook: Build better business applications with NAV

eBook
€27.98 €39.99
Paperback
€48.99
Subscription
Free Trial
Renews at €18.99p/m

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 2009 Programming Cookbook

Chapter 2. General Development

In this chapter, we will cover:

  • Repeating code using a loop

  • Displaying a Progress Bar

  • Checking for conditions using an IF statement

  • Using a CASE statement to test multiple conditions

  • Creating a function

  • Passing parameters by reference

  • Referencing dynamic tables and fields

  • Using recursion

Introduction


Generally developers are not the ones who generate data for their company. Programmers are not employees entering sales orders or new contacts into the system. As a developer, you give users the ability to enter that data, but your main job is to build business logic to manipulate data for the company's benefit.

C/AL, the development language for NAV, is similar to other languages out there. It provides similar commands and functions that other programming languages do. It may not have all of the libraries that .NET does, but C/AL provides all the necessary functions to control data in any way you see fit. The development environment, C/SIDE, is also not very attractive. It does not have all the bells and whistles of Visual Studio, but it has everything you need to get your job done easily. There will be times when you will have to think a little harder about your solution and take a little longer to plan it out, but there is no problem that cannot be solved within NAV.

If you...

Repeating code using a loop


Looping is an essential part of dealing with records in NAV. Using a FOR loop is a common way to iterate over multiple lines of code. This recipe will show you how to construct a FOR loop and use it.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    n

    Integer

    i

    Integer

    Factorial

    Integer

  3. Add the following code to the OnRun trigger of the codeunit:

    Factorial := 1;
    n := 4;
    FOR i := 1 TO n DO
    Factorial := Factorial * i;
    MESSAGE('Factorial of %1 = %2', n, Factorial);
    
  4. Save and close the codeunit.

  5. When you run the codeunit you will 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 to the value 4.

Each time the loop iterates, the value of "i" is increased by one ...

Displaying a Progress Bar


There's nothing more frustrating for a user than wondering if the system is done with processing something or not. Displaying an indicator to show the user the system's progress, is an easy way to make the system more user-friendly.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    ProgressBar

    Dialog

    AmountProcessed

    Integer

    AmountToProcess

    Integer

    PercentComplete

    Integer

  3. Add the following code to the OnRun trigger of your Codeunit:

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

  5. When you run the codeunit you will see a progress bar like this:

How it works...

In order to track the progress of something, you need to know two things: how much you have to do and how much you...

Checking for conditions using an IF statement


Some code should only be executed when certain conditions occur. This recipe will show you how to write code to make that decision.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    Subtype

    SalesHeader

    Record

    Sales Header

    RecordsProcessed

    Integer

     
  3. Write the following code in the OnRun trigger:

    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.

  5. When you run the codeunit you will see a window like the one shown in 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 does. It tells the code that IF you find some records THEN it should do these actions. In this case, the action is to count the records in the...

Using a CASE statement to test multiple conditions


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

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    I

    Integer

  3. Add the following code to the OnRun trigger of your 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. When you run the codeunit you will see a window like 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, is followed by a colon. Here it checks if "i" is equal to 1, if "i" is equal to 2, and if "i" is neither 1 nor 2. You would get the same result if you wrote the following code:

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

Creating a function


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 what functions are in more detail.

How to do it...

  1. Create a new codeunit from Object Designer.

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

  3. Add the following local variables

    Name

    Type

    I

    Integer

  4. Add the following code to your function:

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

    CountToN(3);
    
  6. Save and close the codeunit.

  7. When you run the codeunit you will see several windows like 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 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 to the function.

There's more...

Proper...

Passing parameters by reference


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

How to do it...

  1. Create 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. Add a function called ChangeCustomerName.

  4. The function should take in the following parameter:

    Name

    Type

    Subtype

    Customer

    Record

    Customer

  5. Write the following code in the ChangeCustomerName function:

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

  7. The function should take in the following parameter:

    Name

    Type

    Subtype

    Customer

    Record

    Customer

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

  9. Write the following code in the ChangeCustomerName function:

    Customer.Name := 'Changed Name';
    
  10. Write the following...

Referencing dynamic tables and fields


You may, on occasion, 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. Create a new codeunit from Object Designer.

  2. Add a global function, GetFirstRecord.

  3. The function should take in the following parameter:

    Name

    Type

    TableNo

    Integer

  4. Add the following local variables:

    Name

    Type

    RecRef

    RecordRef

    FieldRef

    FieldRef

  5. Write the following code in your 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
    MESSAGE('Table: %1\%2: %3\%4: %5', RecRef.NAME, FieldRef[1].NAME, FieldRef[1].VALUE, FieldRef[2].NAME, FieldRef[2].VALUE)
    ELSE
    MESSAGE('You cannot retrieve an inactive field.');
    END ELSE
    MESSAGE('No records...

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. Create a new codeunit from Object Designer.

  2. Add a global function, Fibonacci, that returns an integer with no name.

  3. The function should take the following parameter:

    Name

    Type

    i

    Integer

  4. Write the following code in your Fibonacci function.

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

    MESSAGE('Fibonacci(%1) = %2', 4, Fibonacci(4));
    
  6. When you run the codeunit you will see a window like the one shown in 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...

A recursive function has two parts. The first is a stopping condition. In our Fibonacci function, the stopping condition...

Left arrow icon Right arrow icon

Key benefits

  • Write NAV programs to do everything from finding data in a table to integration with an instant messenger client
  • Develop your own .NET code to perform tasks that NAV cannot handle on its own
  • Work with SQL Server to create better integration between NAV and other systems
  • Learn to use the new features of the NAV 2009 Role Tailored Client
  • Easy-to-read recipes with detailed explanations and images
  • Maximize your learning with short tutorials that tell you exactly what you need to know without all of the fluff

Description

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.

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. It would be best if you have gone through a brief introduction to the NAV client. If you are a mid-level NAV developer, you will find the second half more useful. These chapters explain how to think outside of the NAV box when building solutions. There are also recipes here and there that senior developers will find useful.

What you will learn

  • Learn basic programming concepts like variables, loops, and conditionals
  • Build tables and perform complex actions on their data
  • Design different types of forms to display and interact with business data
  • Create reports to present information from multiple areas of the system
  • Write C# .NET code that will work inside the NAV client
  • Learn advanced security techniques including integrating your code with Active Directory
  • Build solutions that work with the entire Microsoft Office suite of products
  • Write code to interact with the Windows file system and registry
  • Create objects to send your data to other applications and read data from other databases
  • Learn to work with SQL Server and execute basic queries against the NAV database
  • Design solutions for the Role Tailored Client

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 20, 2010
Length: 356 pages
Edition : 1st
Language : English
ISBN-13 : 9781849680950
Vendor :
Microsoft

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 : Oct 20, 2010
Length: 356 pages
Edition : 1st
Language : English
ISBN-13 : 9781849680950
Vendor :
Microsoft

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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 €5 each
Feature tick icon Exclusive print discounts
€264.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 €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 134.97
Microsoft Dynamics NAV 2009 Application Design
€36.99
Microsoft Dynamics NAV 2009: Professional Reporting
€48.99
Microsoft Dynamics NAV 2009 Programming Cookbook
€48.99
Total 134.97 Stars icon

Table of Contents

12 Chapters
Strings, Dates, and Other Data Types Chevron down icon Chevron up icon
General Development Chevron down icon Chevron up icon
Working with Tables and Records Chevron down icon Chevron up icon
Designing Forms 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 SQL Server Chevron down icon Chevron up icon
The RoleTailored Client 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.6
(7 Ratings)
5 star 57.1%
4 star 42.9%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Ricardo Nunes Jan 30, 2011
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are a person who wants to start programming in Microsoft Dynamics Nav or want to extend the knowledge then you should buy this book. Nice book with plenty of examples that really helps.
Amazon Verified review Amazon
David Roys Nov 27, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is packed full of useful stuff. I've been doing NAV programming for quite a while now and I know a thing or two, but I found plenty in the book that I could use straight away in my day to day work. The book is divided into 112 recipes that go from the noddy stuff (like basic building blocks of programming NAV) to pretty advanced topics that require creating .NET controls and automations using Visual Studio. It seems like there is no problem too big for Matt. If NAV can't do it, he rolls up his sleeves, cranks up Visual Studio and finds a solution. That was the thing I loved about this book. Matt has a great attitude to working with NAV and shows what can be done if you're prepared to think outside the box.I loved this book. There's something in it for everyone. The recipes are fun-sized morsels of knowledge and you can dib in and out when you need to solve a particular problem, or you can start from the beginning and work your way through if you just want to learn as much as you can.There are plenty of sources of knowledge on NAV now including various books, blogs, and online articles; if you're a completely stingy git, you may prefer to spend your time trawling through forums and trying lots of things rather than shelling out for Matt's book. I know I'd rather save my time and go straight to one reference where I can get an easy step-by-step guide on how to get past my current problem and then move on.If you program NAV, you need this book. Well done Matt!
Amazon Verified review Amazon
Joseph Dewey Dec 29, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is an awesome book. It's so helpful, and I reference it probably once per week. There isn't a ton written about NAV, so if you're serious about NAV, you'll probably want to buy every book available on the current version of NAV. But, I guarantee that you'll reference this book more often than all of the others.This book is written in a "programming cookbook" format, where the book is divided up into about 50 different tasks (recipes) you'd want to do with NAV. And, it's often right on the mark for what you'd actually want to do with NAV.The examples are perfect, too, because you can actually do each one of them with only a few minutes of setup, yet they're substantial enough that they're good to reference even when you're familiar with the subject.And, even though I mainly use this for a reference book now, when I first got it, I went through the first few chapters and did example by example. That was a great way to get familiar with NAV programming very quickly.Pros:+Extremely useful book, I reference it at least once a week+Almost all examples also work for NAV 2013+Very easy to implement examples, which is impressive, since NAV's IDE is kind of clunky.+Long lasting. You'll keep this on your shelf even after you've thrown out your other NAV booksCons:-There isn't a NAV Programming Cookbook 2 yet
Amazon Verified review Amazon
Krupesh Feb 06, 2011
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Microsoft Dynamics NAV 2009 Programming Cookbook is an excellent book where each recipe has been explained in an easy to understand language. This book is highly recommended for those NAV programmers who are looking for tips and tricks for handling real life scenarios. Each code snippet has been explained in detail in simple, easy to understand language and why a particular line of code has been written. This book is a good accompaniment with other basic programming books and the reader needs to have at least basic NAV programming knowledge. After learning basic NAV programming syntax this book will explain how the concepts can be applied.This book is a must-have for all NAV programmers who, at times, face problems and do not know how to go about resolving them. They can then apply the tricks and tricks learnt in this book to resolve real life situations.The book starts with some simple code such as displaying system date and time and gets in to more complex scenarios such as session killer, field level security, interaction with OS, integration, etc. Use of .Net with NAV has been well documented and explained. Matt has demonstrated that sky's the limit for NAV with application of C# code to specific situations.
Amazon Verified review Amazon
Philippe Torres Feb 06, 2011
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Hello,This book is available for all Dynamics NAV 2009 Developper (from the newbies to the senior developper). This book is very helpfull and well organized with tips and tricks. Its a very good choice. I'll recommend it with the purchase of Dynamics NAV 2009 AdminsitrationBonjour,Ce livre est aussi bien utilisable par les néophytes que par les développeurs seniors. Très bien organisé, avec des trucs et astuces, je ne peux que recommander celui-ci. Si vous êtes aussi administrateur, n'oubliez pas Dynamics NAV 2009 Administration.
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.