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 -

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.

Saurav Dhyani


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    ---------------------------

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.

NAV 2013 Upgrade Part III - Data Upgrading.

Hi all, As per agenda we need to discuss the data upgrade in NAV 2013. The process is similar to what used to be till NAV 2009 R2 but with some changes. Let's start the process. Remember -  1) You can only upgrade a database to NAV 2013 from NAV 2009 SP1 / R2 SQL Only. 2) The Synchronization of users with SQL Server is no more required.