Corfu Navision 2016, Development Tips, How To, Information, Tip & Tricks, TryFunction

NAV Design Pattern: TryFunction – .NET Exception Handling in C/AL

When you want to use objects from the .NET Framework in C/AL code, one of the main challenges is to handle exceptions that the methods of these objects may throw. Eventually, if not handled, they will basically bubble up as runtime errors, halting the current operation a user is doing without having a chance to properly display errors in a user-friendly format.

For more details check following links :

https://blogs.msdn.microsoft.com/nav/2015/10/28/nav-design-pattern-tryfunction-net-exception-handling-in-cal/

You can refer my earlier post too using below link :
https://msdynamicsnavashwinitripathi.wordpress.com/2015/10/15/using-try-functions-in-navision-2016/

Advertisement
Development Tips, How To

How to: Set .NET Framework Types to Run on the Microsoft Dynamics NAV Windows Client or Microsoft Dynamics NAV Server Computers

Here is the brief description for how to set .NET Framework objects that are instantiated by DotNet variables to target either the Microsoft Dynamics NAV Windows client or Microsoft Dynamics NAV Server.

By default, a type is set to target Microsoft Dynamics NAV Server. However, you can set the type to target the Microsoft Dynamics NAV Windows client.

Setting the .NET Framework Type to Target the Microsoft Dynamics NAV Windows Client

To set a .NET Framework type instance to target the Microsoft Dynamics NAV Windows client, you must complete the following tasks:

  • Set the DotNet variable for the .NET Framework type to target the Microsoft Dynamics NAV Windows client.
  • Copy the .NET Framework assembly to the computer that is running the Microsoft Dynamics NAV Windows client.

You must copy the assembly to each computer that is running the Microsoft Dynamics NAV Windows client. If the assembly is included in the global assembly cache, then you do not have to do this task.

For the Microsoft Dynamics NAV Web client, you cannot implement Microsoft .NET Framework interoperability objects that target the client.

To set the .NET Framework type to target the Microsoft Dynamics NAV Windows client

  • In Object Designer, open the C/AL code of the Microsoft Dynamics NAV object that uses .NET Framework interoperability.
  • Do one of the following steps:
    • For a global variable, on the View menu, choose C/AL Globals.
    • For a local variable, select the trigger that uses the variable, and then on the View menu, choose C/AL Locals.
    • On the Variables tab, select the DotNet variable, and then on the View menu, choose Properties.
    • In the Properties window, set the RunOnClient property to Yes

To copy a .NET Framework assembly to a computer that is running the Microsoft Dynamics NAV Windows client

    • On the computer that is running the Microsoft Dynamics NAV Windows client, copy the.NET Framework assembly to the Add-ins folder of the Microsoft Dynamics NAV Windows client installation.

By default, the path of this folder is C:\Program Files\Microsoft Dynamics NAV\80\RoleTailored Client\Add-ins or C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client\Add-ins.

You can also put assemblies in a subfolder of the Add-ins folder. This can be useful when you have multiple versions of the same assemblies and dependencies.

Setting the .NET Framework Type to Target Microsoft Dynamics NAV Server

To set a .NET Framework type instance to target Microsoft Dynamics NAV Server, you must complete the following tasks:

  • Set the variable for the NET Framework type to target Microsoft Dynamics NAV Server.
  • Copy the .NET Framework assembly to the computer that is running Microsoft Dynamics NAV Server.

If the assembly is included in the global assembly cache of the computer that is running Microsoft Dynamics NAV Server, then you do not have to do this task.

To set the .NET Framework type to target Microsoft Dynamics NAV Server

  • In the development environment, in Object Designer, open the object that uses the .NET Framework variable.
  • Do one of the following steps:
    • For a global variable, on the View menu, choose C/AL Globals.
    • For a local variable, in the C/AL Editor, select the trigger that uses the DotNet variable, and then on the View menu, choose C/AL Locals.
    • On the Variables tab, select the NET Framework variable type, and then on the View menu, choose Properties.
    • In the Properties window, set the RunOnClient property to No

To copy a .NET Framework assembly to the computer that is running Microsoft Dynamics NAV Server

    • On the computer that is running Microsoft Dynamics NAV Server, copy the.NET Framework assembly to the Add-ins folder of the Microsoft Dynamics NAV Server installation folder.

By default, the path of the Microsoft Dynamics NAV Server installation folder is C:\Program Files\Microsoft Dynamics NAV\80\Service\Add-ins.

You can also put assemblies in a subfolder of the Add-ins folder. This can be useful when you have multiple assemblies and dependencies.

If you are working in the development environment, to compile and test Microsoft Dynamics NAV objects, you must also copy the assembly to the Add-ins folder of the Microsoft Dynamics NAV Windows client installation on the computer that is running the development environment. By default, the path of this folder is C:\Program Files\Microsoft Dynamics NAV\80\RoleTailored Client\Add-ins or C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client\Add-ins.

Development Tips

Automatic Deployment of Microsoft .NET Framework Interoperability and Control Add-in Assemblies

Microsoft Dynamics NAV 2015 makes it easier for the system administrators to deploy client-side assemblies for .NET Framework interoperability and client control add-ins on computers that are running the Microsoft Dynamics NAV Windows client or Microsoft Dynamics NAV Development Environment.

You can now install the assemblies in the Add-ins folder on the computer that is running Microsoft Dynamics NAV Server. By default, this is the C:\Program Files\Microsoft Dynamics NAV\80\Service\Add-ins folder.

When an operation from the client requires an assembly, Microsoft Dynamics NAV Server automatically deploys the assembly to a temporary folder on the client computer.

For example, if Microsoft Dynamics NAV Windows client opens a page that contains a control add-in, Microsoft Dynamics NAV Server will find the control add-in assembly by name in the Add-ins folder. Then, it deploys the assembly to the client computer in the %TEMP%\Microsoft Dynamics NAV\Add-Ins folder of the user who is running the client. Subsequently, the deployed assembly will be used whenever the page is opened.

Similarly, if the development environment requires a control add-in, for example, when you compile an object, then the control add-in assembly will be deployed by the Microsoft Dynamics NAV Server to the local temporary folder for the current user on the computer that is running the development environment.

Important:

To be deployed, an assembly must comply with the following Microsoft Dynamics NAV Server configuration settings: Chunk Size, Max Upload Size, and Prohibited File Types.

If a .NET Framework interoperability or control add-in assembly is updated and its version number changes, Microsoft Dynamics NAV Server will deploy the updated assembly to the client computer the next time that the client requests the assembly. The updated assembly is put in a subfolder of the %TEMP%\Microsoft Dynamics NAV\Add-Ins folder, where the subfolder has the assembly’s version number as its name. This implementation means that you do have to remove the older versions of assemblies that are stored on the client computer.

To support compatibility with earlier version of Microsoft Dynamics NAV, before Microsoft Dynamics NAV Server deploys an assembly to a client, the client looks for the assembly in the local Add-ins folder (for example, C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client\Add-ins). If the assembly is not found, then the client will request the assembly from Microsoft Dynamics NAV Server.

Javascript-based client add-ins have been using this deployment technique since Microsoft Dynamics NAV 2013 R2.

Development Tips

Learning .NET Framework interoperability by Example

Today we will discuss an example which uses .NET Framework interoperability to display headlines from the RSS feed of my site https://msdynamicsnavashwinitripathi.wordpress.com/

This example accesses classes in the System.XML assembly that is found in Global Assembly Cache.

Today’s example uses .NET Framework interoperability to display headlines from an RSS feed from my blog site, which has the following URL:

https://msdynamicsnavashwinitripathi.wordpress.com/feed/

We will use members of the System.XML assembly, which is part of the Microsoft .NET Framework class library and is installed in the global assembly cache.

Let’s start with creating a table to store the value of feeds as below:

DotNetInteroperability-1
Save your Table.

Next we will be creating a codeunit that has the following local variables:

DotNetInteroperability-2

Variable name DataType SubType
xml DotNet ‘System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.Xml.XmlDocument
items DotNet ‘System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.Xml.XmlNodeList
I Integer
title DotNet ‘System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.Xml.XmlNode
FeedTable Record DotNetInteroperability

After you create the codeunit, add a function LoadFeed and add below code to it:

 DotNetInteroperability-3
xml := xml.XmlDocument();

xml.Load(‘https://msdynamicsnavashwinitripathi.wordpress.com/feed/?format=xml’);

items := xml.SelectNodes(‘/rss/channel/item’);

FOR i := 0 TO items.Count – 1 DOBEGIN

title := items.Item(i).SelectSingleNode(‘title/text()’);

FeedTable.INIT;    FeedTable.”Topic ID” := i + 1;

FeedTable.”Topic Description” := title.Value;

IF NOT FeedTable.INSERT THEN

FeedTable.MODIFY;

END;

MESSAGE(‘Loading of Feeds Done.’);

Save your codeunit.

To see the example in the Microsoft Dynamics NAV Windows client, you will now create an action on a page that opens the codeunit.

Create a Page as below:

DotNetInteroperability-4
Create below Variable of codeunit created above:

DotNetInteroperability-5
Create below Page Actions:

DotNetInteroperability-6
Write a code for Load Feed Action as:

DotNetInteroperability.LoadFeed;

Save your Page.

Now Run this Page:

DotNetInteroperability-7
Click the Action Load Feed created above.

DotNetInteroperability-8
Respond OK to see the feeds loaded. Make sure your internet connection is operational as it directly fetch the Live feed from Internet.

DotNetInteroperability-9
In this case it will Load 50 Posts headings as this feed provides 50 as output, different feeds may return different numbers mostly 10-20 posts.

Using above logic you can create any such functionality and add value to your customers.

Thanks for going through this post, hopefully you may have earned some fruitful Tips.

You can follow the blog or subscribe to the RSS feed to remain updated and keep getting such posts in future.

Your response to my posts will keep motivating me for helping the community.