Skip to main content

MSDYN365BC - AL Development 06 - How to Develop Subscriber Codeunits?

Hi Readers,

I am sure everyone was able to publish your extension based on the Last article. If you haven't read the earlier article, then refer to the table of content.

As promised in the article we will add code to check Source of sales is filled in during the posting of sales order as per customer requirements.

The user should be able to select the Source of sales in Sales documents and its a mandatory field to key in before posting a sales document.

Let's try to add this Code to our extension.
Let's go back to our C/AL Knowledge - At what point during sale order posting, Microsoft checks the document?

If you remember Microsoft use a function in Codeunit 80 - "CheckAndUpdate". Let's see do we an event similar to that in Codeunit 80.

Now, this is not Codeunit Extension. We will be subscribing events that are defined in Codeunit 80 so we will not call new file as Codeunit extension. We will call this Subscribing Codeunit.

In one Codeunit we can subscribe to multiple events defined in multiple base objects. In this case, I would like to keep all my related code segment in one Codeunit. This Codeunit will have all subscribers.

Add a new folder "Codeunit" & a file on the Left-hand Panel - right click & select New File as shown below.
As per our standard page file name is - SDH_Codeunit_50000_ExtensionBlogSubscribers.al


Let's follow the routine and use snippet tcodeunit to get the structure of a Codeunit.


Next Step to set these Page Properties -
Assign ID and
Provide a name to the Codeunit that you are adding.


As you can see in above image, snippet added default onRun Trigger in the Codeunit and a Global variables section which was added.

Next is to add subscriber for Codeunit 80 Events. We will use snippet teventsub to get the skeleton for subscribing as shown below.


Now once we select that snippet we will see the parameters that require to be defined as shown below.


Parameters are -


Remember it like this -If something happens, in some object name of some object type then call the procedure. Below are the details which should help too -


Let's Start assigning values to parameter -

  1. ObjectType::Codeunit
  2. Codeunit::"Sales-Post"
  3. OnAfterCheckSalesDoc - Event that we want to subscribe.
  4. '' - ** This parameter only applies to Table Fields.
  5. TRUE
  6. TRUE
  7. CheckSalesOrderSourceofSales - Function Name that we want to name the function.


Next Question that you might be thinking - We know that CheckSalesOrderSourceofSales will be called when sales order posting routine is called but how can I check the record of sales Header?


  • Every event that is published will have some parameters that we can utilize in our function/procedure. Let's see how can I check what parameters are available.
  • In Procedure parameter, if you click Ctrl+Spacebar system will show you all available parameter available as shown below.


If you notice record variable passed are passed as VAR that means if we modify those records here it will update actual record. So I will add all available variables and write our code. To add the available parameter you can use ; as the separator as shown below.



The last thing is to add code for which we all are experts 😎

With this code system will check Source of sales is mandatory while posting sales order.



Hope it makes sense and you learn how to subscribe to Events published.

Do share your views as comments to this article. Next article we will add more events in the same Codeunit to complete user requirements.

Stay Tuned.

Regards,
Saurav Dhyani
www.sauravdhyani.com

Comments

Popular posts from this blog

VIEW SERVER STATE permission on SQL Server?

Hi all, Sometime While trying to Login into a database we face an error message as shown below. --------------------------- Microsoft Dynamics NAV Classic --------------------------- You cannot start Microsoft Dynamics NAV Classic because you do not have the VIEW SERVER STATE permission on SQL Server. Contact your system administrator. --------------------------- OK    ---------------------------

RTC Report It is not possible to instantiate the Visual Studio bridge.

Hi all, As a Navision developers I have Multiple Versions of Navision running in single Machine. As discussed Earlier in the post how to run multiple Version of RTC in single machine. So my machine have following details for RTC Versions - NAV 2009 R2    - is installed. NAV 2009 SP1  - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 SP1 IN\ NAV 2009         - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 IN\ This approach has been working great for execution of Classic and RTC Clients. However, after installing Dynamics NAV 2009 R2, if i tried to view the Layout for an NAV 2009 SP1 Report i was getting the following error: ---------------------------  Microsoft Dynamics NAV Classic  ---------------------------  It is not possible to instantiate  the Visual Studio bridge.  ---------------------------  OK    --------------------------- After searching for the error i figured out the issue was due to - Design change in NA

NAV 2013 R2 - Cumulative update 12 Released.

Hi all, Please find below the details of  Cumulative Update 12 released for Microsoft Dynamics NAV 2013 R2. Title - Cumulative Update 12 for Microsoft Dynamics NAV 2013 R2 Build No. - 38053 Release Date - October, 2014 Local Version Included - AU, AT, BE, CH, DE, DK, ES, FI, FR, IS, IT, NA, NL, NO, NZ, SE, UK, RU Download Link Note: Implementing this cumulative update will require a database conversion unless you have already implemented update rollup 5.