Book Image

Microsoft Dynamics NAV Development Quick Start Guide

By : Alexander Drogin
Book Image

Microsoft Dynamics NAV Development Quick Start Guide

By: Alexander Drogin

Overview of this book

Microsoft Dynamics NAV is an enterprise resource planning (ERP) software suite for organizations. The system offers specialized functionality for manufacturing, distribution, government, retail, and other industries. This book gets you started with its integrated development environment for solving problems by customizing business processes. This book introduces the NAV development environment – C/SIDE. It gives an overview of the internal system language and the most essential development tools. The book will enable the reader to customize and extend NAV functionality with C/AL code, design a user interface through pages, create role centers, and build advanced reports in Microsoft Visual Studio. By the end of the book, you will have learned how to extend the NAV data model, how to write and debug custom code, and how to exchange data with external applications.
Table of Contents (10 chapters)

Exporting and deploying NAV objects

To exchange objects between different databases, it is possible to export them in an external file and import it into a new destination. NAV supports two file formats for application objects: .txt and .fob. The former is object source code in plain text format, while .fob is a native NAV format containing compiled code. Since objects in the .fob file are already in the compiled state, they don't have to be recompiled after import in order to be executed. Objects imported from text files must be compiled first; otherwise, they cannot be run.

Another significant difference between the two object formats is the license permission required to work with files. To be able to export and import plain text files, you need a developer's license, which allows you to create and edit specific objects being processed. The .fob file format does not impose such restrictions on the file exchange process. This is the reason why .fob files are typically used to export new or modified objects from a development/test environment and deploy them to a production server. Usually, the production server is run under an end user license without development access to application code, and there is no possibility to import plain text files.

Besides, software developers usually don't want to disclose source code when selling their solutions. The native .fob format hides the application source behind compiled bytecode, which is impossible with .txt files.

In the Object Designer window, select the 50500 codeunit you created earlier and choose the menu option File | Export, then select the file location. In the File Type field, choose *.txt as the file format, and press the Save button. This will create a file containing the source code for the codeunit:

OBJECT Codeunit 50500 Hello World
{
OBJECT-PROPERTIES
{
Date=14.08.18;
Time=16:39:41;
Modified=Yes;
Version List=PACKT QSG;
}
PROPERTIES
{
OnRun=BEGIN
MESSAGE('Hello World');
END;
}
CODE
{
BEGIN
END.
}
}

To import the object into another database, open a copy of the demo database and invoke the File | Import command from the main menu. Select the exported file and click Open.

If an object being imported from a text file already exists in the database, the new version will replace the old one without a confirmation request. Be careful and double-check that the database object can be safely deleted.

Managing objects with the Dynamics NAV Development Shell

Similar to the NAV 2018 Administration Shell, the NAV 2018 Development Shell provides access to PowerShell cmdlets for the management of application objects. Development tasks, such as object export, import, and compilation, can be automated with the Development Shell. The same as Administration Shell, NAV Development Shell can be run from the Start menu. On startup, it imports several modules. We will now take a closer look at one of them: Microsoft.Dynamics.Nav.Model.Tools.

To list all cmdlets available in the Microsoft.Dynamics.Nav.Model.Tools module, run this:

Get-Command -Module Microsoft.Dynamics.Nav.Model.Tools

As an alternative to the method described previously, the import/export procedure can be performed in the NAV Development Shell. To do it, run the following commands one by one.

The first cmdlet will export objects defined by the Filter parameter from the specified NAV server to the disk:

Export-NAVApplicationObject -DatabaseName "Demo Database NAV (11-0)"
-DatabaseServer "localhost\NAVDEMO" -Path "C:\NAV Objects\COD50500.txt"
-Filter "Type=Codeunit;ID=50500"

Typically, you apply a Filter based on the object type and name, but other object properties can be used for filtering as well. For example, you may want to export all modified objects:

Export-NAVApplicationObject -DatabaseName "Demo Database NAV (11-0)"
-DatabaseServer "localhost\NAVDEMO"
-Path "C:\NAV Objects\Modified.txt" -Filter "Modified=Yes"

Or export all object labeled with the version PACKT:

Export-NAVApplicationObject -DatabaseName "Demo Database NAV (11-0)"
-DatabaseServer "localhost\NAVDEMO"
-Path "C:\NAV Objects\COD50500.txt" -Filter "Version List=*PACKT*"

To import the objects, run the Import-NAVApplicationObject cmdlet:

Import-NAVApplicationObject -Path "C:\NAV Objects\COD50500.txt"
-DatabaseName "Demo Database NAV (11-0)" -DatabaseServer "localhost\NAVDEMO"

Imported objects must be compiled in the new database:

Compile-NAVApplicationObject -DatabaseName "Demo Database NAV (11-0)"
-DatabaseServer "localhost\NAVDEMO" -NavServerName DynamicsNAV110
-Filter "Type=Codeunit;ID=50500"

You can compile all objects in the database, or use the Filter parameter to select a subset.

Now, let's combine all these code samples into one Powershell function that can export, import, and compile the object with a single command. To do this, we will need the PowerShell IDE, which can be started from the Windows Applications menu. The application name is powershell_ide. Place the following code in the PowerShell editor and run it:

if ([Environment]::Is64BitProcess)
{
$RtcFolder =
'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft Dynamics NAV\110\RoleTailored Client'
}
else
{
$RtcFolder = 'HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\110\RoleTailored Client'
}

Test-Path $RtcFolder
$IdeModulePath = (Join-Path (Get-ItemProperty $RtcFolder).Path Microsoft.Dynamics.Nav.Ide.psm1)
Import-Module $IdeModulePath

function Deploy-ObjectsToTestServer
{
Param(
[Parameter(Mandatory = $true)]
[string] $DevelopmentDatabaseName,
[Parameter(Mandatory = $true)]
[string] $TestDatabaseName,
[Parameter(Mandatory = $true)]
[string] $DevelopmentServerName,
[Parameter(Mandatory = $false, ParameterSetName =
"SeparateTestServer")]
[string] $TestServerName,
[Parameter(Mandatory = $false, ParameterSetName =
"SingelServerSetup")]
[switch] $SingleServer = $true,
[Parameter(Mandatory = $true)]
[string] $NavServerInstance,
[Parameter(Mandatory = $true)]
[string] $ObjectFilter
)

if ($SingleServer)
{
$TestServerName = $DevelopmentServerName
}

[string] $tempFileName = [System.IO.Path]::GetTempFileName() +
".txt"

Export-NAVApplicationObject
-DatabaseName $DevelopmentDatabaseName -DatabaseServer
$DevelopmentServerName
-Path $tempFileName -Filter $ObjectFilter
Import-NAVApplicationObject
-Path $tempFileName -DatabaseName $TestDatabaseName -
DatabaseServer $TestServerName
Compile-NAVApplicationObject
-DatabaseName $TestDatabaseName -DatabaseServer $TestServerName
-NavServerName $NavServerInstance -Filter $ObjectFilter
}

Now you can easily prepare your modification for testing with a single command:

Deploy-ObjectsToTestServer -DevelopmentDatabaseName "Demo Database NAV (11-0)" -TestDatabaseName "NAV 110 Test Database" -DevelopmentServerName localhost\NAVDEMO -SingleServer -NavServerInstance "NavTestServer" -ObjectFilter "ID=50500..50599"

This command will export all objects with IDs from 50500 to 50599 from the development database, Demo Database NAV (11-0), into a temporary file, import it into the NAV 110 Test Database, and compile all imported objects.