Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Dynamics NAV Development Quick Start Guide

You're reading from   Microsoft Dynamics NAV Development Quick Start Guide Get up and running with Microsoft Dynamics NAV

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher Packt
ISBN-13 9781789612769
Length 210 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alexander Drogin Alexander Drogin
Author Profile Icon Alexander Drogin
Alexander Drogin
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Getting Started with the NAV Development Environment FREE CHAPTER 2. Codeunits - Structuring C/AL Code 3. Tables - Creating Data Structure 4. Designing User Interface 5. Exchanging Data with XML Ports 6. NAV Event Model 7. Presenting Data in Reports 8. Debugging Your Code 9. Other Books You May Enjoy

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.

You have been reading a chapter from
Microsoft Dynamics NAV Development Quick Start Guide
Published in: Dec 2018
Publisher: Packt
ISBN-13: 9781789612769
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime