Building our application

We can use our Delta file to build our application, based on a new version of the software that also uses PowerShell commands.

PowerShell is, as the name implies, a very powerful tool that not only supports Dynamics NAV, but most Microsoft applications including the SQL Server. This allows us to generate a single script that restores a new SQL Database, applies our Delta file, compiles objects, and runs the test.

Let's go through this process step by step using an example script. In this script, the parameters are hardcoded for readability. It is recommended that you use variables instead.

  1. Prepare: the first step is to prepare some mandatory variables, such as a reference to the NAV Ide. Next, load the libraries that we need. We need the SQL Server library and both the NAV libraries:
    $NavIde = "C:Program Files (x86)Microsoft Dynamics NAV80RoleTailored Clientfinsql.exe"
    
    Set-ExecutionPolicy unrestricted
    
    import-module "C:Program Files (x86)Microsoft Dynamics NAV80RoleTailored ClientNavModelTools.ps1"
    import-module "C:Program FilesMicrosoft Dynamics NAV80ServiceNavAdminTool.ps1"
    
    Push-Location
    import-module sqlps
  2. Restoring database: the intention is to start with a fresh copy of the Dynamics NAV database. We do so by stopping the service tier, dropping the existing database, and restoring a new database. After the restore, we connect the service tier user to the database and restart the service tier:
    Set-NAVServerInstance "bedandbreakfast" –Stop
    
    invoke-sqlcmd -ServerInstance ".NAVDEMO" -Query "ALTER DATABASE [BedAndBreakfast (8-0)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
    invoke-sqlcmd -ServerInstance ".NAVDEMO" -Query "DROP DATABASE [BedAndBreakfast (8-0)]"
    invoke-sqlcmd -ServerInstance ".NAVDEMO" -Query "RESTORE DATABASE [BedAndBreakfast (8-0)] FROM DISK = N'E:Demo Database NAV (8-0).bak' WITH FILE = 1, MOVE N'Demo Database NAV (8-0)_Data' TO N'C:SQLFilesDemo Database NAV (8-0)_Data.mdf', MOVE N'Demo Database NAV (8-0)_Log' TO N'C:SQLFilesDemo Database NAV (8-0)_Log.ldf', NOUNLOAD, STATS = 5"
    
    Invoke-Sqlcmd -ServerInstance ".NAVDEMO" -Database "BedAndBreakfast (8-0)" -Query "CREATE USER [NT AUTHORITYNETWORK SERVICE] FOR LOGIN [NT AUTHORITYNETWORK SERVICE]"
    
    Invoke-Sqlcmd -ServerInstance ".NAVDEMO" -Database "BedAndBreakfast (8-0)" -Query "exec sp_addrolemember 'db_owner', 'NT AUTHORITYNETWORK SERVICE'"
    
    Set-NAVServerInstance "bedandbreakfast" –Start
  3. Exporting the text files: since we cannot import the Delta files directly into the NAV database, we have to generate a set of text files to apply the Delta files. This is done using the PowerShell commandlets:
    Export-NAVApplicationObject -DatabaseName "BedAndBreakfast (8-0)" -Path "E:BandBTarget.txt" -DatabaseServer ".NAVDEMO"
    
    Split-NAVApplicationObjectFile -Source "E:BandBTarget.txt" -Destination "E:BandBTarget" -Force
  4. Applying the Delta file: the next step is the actual merge between the files, and the creation of the result file that we can import into NAV. We remove all objects that are untouched to reduce the amount of objects that are to be compiled:
    $MergeResult = Update-NAVApplicationObject -TargetPath "E:BandBTarget"
      -DeltaPath "E:BandBDelta" `
      -ResultPath "E:BandBResult" -Force
    
    $MergeResult | Where-Object UpdateResult -eq Unchanged | foreach { Remove-Item -Path $_.PSPath } -Verbose‏
    
    Join-NAVApplicationObjectFile -Source "E:BandBResult" -Destination "E:BandBResult.txt"
  5. Importing and Compiling: assuming the file has been generated without conflicts as we designed, we can import the result in the database and compile the objects:
    Import-NAVApplicationObject -DatabaseName "BedAndBreakfast (8-0)" -Path "E:BandBResult.txt" -DatabaseServer ".NAVDEMO"
    
    Compile-NAVApplicationObject -DatabaseName "BedAndBreakfast (8-0)" -DatabaseServer ".NAVDEMO" -Filter 'Compiled=No'
  6. Testing the result: the last and final build step is to run the Test Codeunit via PowerShell. We do so by invoking the Codeunit:
    Invoke-NAVCodeunit -ServerInstance "bedandbreakfast" -CodeunitId 84099 -CompanyName "CRONUS International Ltd."

Building the result

The result of the build should look something like the next screenshot. In our case, the build failed because one of the tests failed, as shown in the following screenshot:

Building the result
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset