Showing posts with label Implementing. Show all posts
Showing posts with label Implementing. Show all posts

Friday, 9 October 2015

Implementing Events in Navision 2016

Recall from my earlier post for description of Events - Events in C/AL Navision 2016

Here in this post we will see how to implement the same.

This could be helpful in situation where throughout the system you wish to have similar behaviour on specific condition. In general we will have to write code in every object with same set of logic, variables, and functions and so on.

Although still in this case too we will require to add code to every object, but only one time.

Think of you need to enhance or make changes in behaviour again you will have to go through each objects and do the required change. But using this feature we can keep it centralized and manage from single place, without going through each objects again.

Here below I am taking example one explained by Microsoft help basic concept, will come up with my version with some more effective usage later sometime.

When users change the address of a customer, you want to check that the address does not include invalid characters, which in this walkthrough is a plus sign (+).

To accomplish this, you will publish an event that is raised when the Address field on page 21 Customer Card is changed.

To handle the event when it is raised, you will add an event subscriber function that includes logic that checks the address value and returns a message to the user if it contains a plus sign.

Publishing the Event

To publish an event, you create a C/AL function that is set up to be an event publisher. An event publisher function can be added in any object, such as a codeunit, page, or table.

In this procedure we will add the event publisher function to a new codeunit, in which you can potentially add more event publisher functions for other events later.

Because you might want to change this event implementation in the future, you decide to create an integration event type.

The event publisher requires a single text parameter for handling the address of the customer.

Create a new codeunit

The codeunit for the event publisher I have created 50000 - Event Publisher.

Now we will add the event publisher function to publish the event.
Events-1

To create the event publisher function to publisher the event

  • Define a function OnAddressLineChanged.

  • Open the properties for the OnAddressLineChanged function, select the function, and then in the View menu, choose Properties. Set the properties as follows:



  • Set the Local property to No.


Setting this property makes the function available to be called from the other objects.

  • Set the Event property to Publisher. This makes the function an event publisher.

  • Set the EventType property to Integration.

  • Close the Properties


Events-2

  • Add a local parameter to the function for the address of the customer as described in the following steps:



  • On the Functions tab, select the OnAddressLineChanged function, and then choose the Locals


The C/AL Locals window opens.

  • On the Parameters tab, in the Name field, enter line.

  • Set the DataType field to Text.

  • Set the Length field to 100.


Events-3

Important

An event publisher function cannot have a return value, variables, or text constants; otherwise you will not be able to compile the function.

The new function appears in the C/AL Editor with the following signature:

[IntegrationEvent] OnAddressLineChanged(line : Text[100])
Events-4

You can now raise the event in the application.

Raising the Event

After creating the event publisher function to publish the event, now we will add code to the application to raise the event where it is required.

In this case, the event will raise when the Address field is changed on the page 21 Customer Card.

Therefore, we will add code to the Address - OnValidate() trigger in C/AL code of the page. Raising an event basically involves calling the event publisher function that publishes the event.

To raise the event

  • In the development environment, open page 21 Customer Card as follows:

  • Add a C/AL variable that specifies the object that publishes the event. In this case, the event publisher object is codeunit 50000 Event Publisher, which contains the event publisher function OnAddressLineChanged that you created in the previous procedure.


Events-5

  • In C/AL code, add the following code on the Address - OnValidate() trigger to raise the event:


Publisher.OnAddressLineChanged(Address);

This calls the event publisher function to raise the event.
Events-6

  • Save and compile the changes to the page.


The event can now be subscribed to and handled.

Subscribing to and Handling an Event

Once an event has been published you can add code to the application that subscribes to and handles the event when it is raised.

For example, in this case when a user changes the address of a customer (the event), you want code that checks that the value does not contain a plus sign.

Subscribing to and handling an event is accomplished by creating a C/AL function that is set up as an event subscriber and subscribes to a specific event (defined by an event publisher function). The event subscription function contains the application logic for handling the raised event.

In this case, we will create an event subscriber function that subscribes to the OnAddressLineChanged function in codeunit 50000 Event Publisher.

Unlike an event publisher function, an event subscriber function can only reside in a codeunit object. This procedure will add the event subscriber function to a new codeunit, in which you can potentially add more event subscriber functions for other events later.

To create a new codeunit

  • In the development environment, create a new codeunit that has the ID 50001 and the name Event Subscriber.


To create the event subscriber function to subscribe to and handle the event

  • Create Function CheckAddressLine.


Events-7

  • Choose Properties. Set the properties as follows:



  • Set the Event property to Subscriber to make the function an event subscriber.

  • Set the EventPublisherObject property to Codeunit My Publishers.


This is the codeunit that contains the event publisher function (OnAddressLineChanged) that you want to subscribe to.

  • In the EventFunction property, select the OnAddressLineChanged integration event.


This field reads all the published events in the event publisher object.
Events-8

Note

When you get a message that asks whether you want to overwrite the edited function's signature, choose Yes to continue.

  • A local parameter that has the name line and the data type Text has been automatically added to the new CheckAddressLine

  • The new function appears in the C/AL Editor with the following signature:


LOCAL [EventSubscriber] CheckAddressLine(line : Text[100])

You can now add code to handle the event.

  • To handle the event, add the following code to the CheckAddressLine function in the C/AL editor:


IF (STRPOS(line, '+') > 0) THEN BEGIN

ERROR('Cannot use a plus sign (+) in the address [' + line + ']');

END

This code checks the value of the Address field on page 21 Customer Card when is has been changed and returns a message if the value contains a plus sign.
Events-9

Viewing the New Event Subscription

After you create an event subscriber, you can view information about it in page 9510 Event Subscriptions. This page provides information about all the current event subscriptions in the application. You can open this page directly from the development environment or from a Microsoft Dynamics NAV client.

To view the event subscription from the development environment

  • On the Tools menu, choose Debugger, and then choose Event Subscriptions.


Events-10

To view the event subscription from a Microsoft Dynamics NAV client

  • Start the Microsoft Dynamics NAV client.

  • In the Search box, enter Sessions, and then choose the related link.


Events-11

The Even Subscription Window will look like:
Events-12

Testing the Event

To test the event implementation, you can run page 21 Customer Card from the development environment.

  • In the Address field, add a plus sign, and then choose the OK


The following message appears:

Cannot use a plus sign (+) in the address [].

[] contains the value of the Address field.
Events-13

I will come up with more details on this topic later in my future posts.

Friday, 7 August 2015

Implementing Security Certificates to support login over a WAN

Today we will set up an environment to test integrating certificates into a configuration for connecting computers running the Microsoft Dynamics NAV Windows client or Microsoft Dynamics NAV Web Server components to Microsoft Dynamics NAV Server instances over a wide area network (WAN) with the chain trust configuration.

What all we need to get our today’s exercise done?

To complete this process, we will need:

  • The Certificates snap-in for Microsoft Management Console (MMC). If you do not already have this snap-in installed, then follow these steps:




    • Choose Start, choose Run, and then type Mmc.exe.

    • In the console, on the File menu, choose Add/Remove Snap-in.



Certificate-1


    • In the Add Standalone Snap-in dialog box, select Certificates, choose Computer Account, choose Local Computer, and then choose Add.



Certificate-2

Certificate-3

  • The makecert.exe utility. The makecert.exe utility is installed with Microsoft Visual Studio and Microsoft Windows SDK.


We created and installed a root certification authority (CA) and a server certificate on the computer running Microsoft Dynamics NAV Server in previous posts.

If not please follow below posts and then return here to continue.

Previous Step Link- How to create a root CA and a private key file by using the makecert.exe utility

Previous Step Link- How to create certificate revocation list for the root certification authority

Previous Step Link- How to create and install a test certificate for the Microsoft Dynamics NAV Server computer

Previous Step Link- How to grant access to the certificate’s private key to the service account for the Microsoft Dynamics NAV Server

How to modify the Microsoft Dynamics NAV Server configuration file to support login over a WAN

  • Start the Microsoft Dynamics NAV Server Administration tool.

  • Stop the Microsoft Dynamics NAV Server instance.


Certificate-17

  • Modify the following settings for the Microsoft Dynamics NAV Server instance in the Microsoft Dynamics NAV Server Administration tool.




















Key New value Description
ClientServicesCredentialType(General tab)NavUserPassword or UsernameThis parameter is on the General tab in the Microsoft Dynamics NAV Server Administration tool. The default value is Windows. When you change it to NavUserPassword or Username, client users who connect to the server are prompted for user name and password credentials.
CertificateThumbprint(Client Services tab)Value of the Thumbprint field in the previous postThe default value is <key>. Remove any leading or trailing spaces in the thumbprint.

Certificate-18

  • Choose Save.

  • Restart the Microsoft Dynamics NAV Server instance.


To modify the Microsoft Dynamics NAV Windows client configuration file to add certificate information

  • Open the ClientUserSettings.config configuration file.


The location of this file is Users\<username>\AppData\Roaming\Microsoft\Microsoft Dynamics NAV.

By default, this file is hidden. Therefore, you may have to change your folder options in Windows Explorer to view hidden files.
Certificate-19

  • Modify the following settings.




















Key New value Description
ClientServicesCredentialTypeNavUserPassword or UsernameThe default value is Windows. When you change it to NavUserPassword or Username, client users who connect to the server are prompted for user name and password credentials
DnsIdentityThe subject name of the service certificateThe default value is <identity>.For this exercise, set the key to NavServiceCert.

Certificate-20
I am using NavUserPassword as credential Type in this example.

  • Save and close the ClientUserSettings.config file.


When you start the client, you are prompted for a valid user name and password for the server domain.
Certificate-21

Enter your User Name and Password to Login, I have created one for my testing as below:
Certificate-22
To modify the Microsoft Dynamics NAV Web client configuration file to add certificate information

  1. On the computer that is installed the Microsoft Dynamics NAV Web Server components, open the installation folder for the Microsoft Dynamics NAV Web client. By default, the folder is C:\Program Files\Microsoft Dynamics NAV\80\Web Client.

  2. Open the web.config file in a text editor, such as Notepad.

  3. Find the <DynamicsNavSettings> element, and then change the following settings:




















Key New value Description
ClientServicesCredentialTypeNavUserPassword or UsernameThe default value is Windows. When you change it to NavUserPassword or Username, client users who connect to the server are prompted for user name and password credentials.
DnsIdentityThe subject name of the service certificateFor this exercise, set the key to NavServiceCert.


  1. Save the web.config file.


This too works as above, credentials will be asked when you open WebClient.

While writing this post I am not having Web Client installed on my computer so unable include screen shots for same.