Tuesday, February 5, 2019

MSDYN365BC - Futuristic CAL Development - What If No Event Found?

Hi Readers,


Last article we Understand what is Integration Events.
As promised in this post we will understand if you don't find an event because you are not in Latest Version of NAV/Business Central or you are in a version which does not support Events (Pre-NAV 2016).

If you are new to this series then Please Refer Table of Index.

With this article, We will understand about HOOKS and Use Hooks for development.

This will help Developers to develop with keeping in mind about future and all code can be easily transported to future when your customer plan to Upgrade. Let's discuss that in details.



What we Need?
We always should have one Microsoft Standard Database installed on our local machine right now that is Microsoft Dynamics Business Central on-Prem Cumulative Update 03.

Why we need that?
As discussed in the Last article Microsoft keeps on and will keep on adding Integration Events in future. If we have the latest cumulative update of Business Central installed we will know which all events are available in future for our custom Code.

What is a Hook?
In programming, a hook is a place and usually an interface provided in a packaged code that allows a programmer to insert customized programming. For example, a programmer might want to provide code that analyzed how often a particular logic path was taken within a program.

Why use Hook?
As a partner, adding new code to NAV means interfering with code shipped by Microsoft. Minimize your footprint of changes in Microsoft code, so that, when a new NAV version is shipped, you avoid conflicts and upgrade impact. The core NAV code is the "danger zone"  - the less you touch it, the happier your upgrade will be.

Read More About Design Pattern
Read More About Hook Pattern


Hook Example - 
Consider the same example but our customer is using 2009 (which they should not use now 😃).

We want to copy Expense Code from G/L Account Table to Purchase Line When No. Get validated in Purchase Line table.

Let's Create a New Codeunit which will store all Hooks for this customization, as shown below.
Add a new function which is similar to Publisher in future. (Look in Latest CU).
Add parameters as available in Future Publisher. (Look in Latest CU).
Add Custom Code that we want to Execute in Base Objects.



Now as its not a Publisher in our customer database, we will have to write one line of custom code in the Base Objects that invokes this Hook as shown below.


In this example, we just wanted to add one line of code so it may seem like a bad idea but think of an example where you as developer write multiple lines of code in base objects. This will become a Boon for you in the future.

I hope you would have got the Idea about HOOKS and how to use them.

If you have any question, please add that as the comment to the article.

Next article, we will continue with our customer requirements.

Stay Tuned for More!!

Regards,
Saurav Dhyani

5 comments:

  1. Thanks, Saurav. I just want to make sure I understand this properly. Are you saying that when using hooks we are in fact modifying base NAV objects? Up until now I thought this was going to be absolutely prohibited. If so, what happens when Microsoft upgrades the object in a future CU? Wouldn't our hook be lost? Does this mean we have to manage our hooks and make sure they are re-established during each CU?

    ReplyDelete
    Replies
    1. @Justin - SO you will only use HOOKS till you are not using the latest version of NAV / Business central. If you plan to use HOOKS then you will have to maintain (merge) them during upgrade or cumulative update.

      I am suggesting hooks so that footprint of custom code in base objects (even in older versions) is less and these hooks will be replaced to Subscribers during upgrades to latest version. Once you are there lets say in Business central on Prem then NO HOOKS only SUBSCRIBERS.

      HOOKS Is for PRE-BUSINESS CENTRAL if you dont find appropriate event and you already checked that event exist in future.

      Hope it make sense. Let me know if any doubts.

      Delete
    2. Got it. Thanks for the clarification.

      Delete