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

Send Mail with Attachment From Navision.

Hi all, We have seen how to save a report into PDF and how to send mail to a customer. Let's link these two post in one i.e. Mailing statement to a customer into PDF Format. This article is part of the Series. Please Refer  Table of Content here . If you have the old objects set let me brief you what I will be changing - 

MSDYN365BC - Data Upgrade To Microsoft Dynamics 365 Business Central on premises.

Hi Readers, We have already talked about the number of steps for upgrading to Business Central on Premises from different NAV versions. After that article, I received multiple requests for an article which list down steps for Data Migration. In this article, we will discuss steps of data migration to MSDYN365BC (on-Prem) from NAV 2017. For this article, I am considering a Cronus Demo Database without any customization. For an actual upgrade project, we will have to complete object merge using compare and Merge process. After the Merge Process, the next step is data migration. Let's discuss those steps. Direct Upgrade to Microsoft Dynamics 365 Business Central (on-Prem) is from following versions - 1. NAV 2015. 2. NAV 2016. 3. NAV 2017. 4. NAV 2018.

BC 21 and Higher - PowerShell Cmdlet (Replacement of Business Central Administration).

Hi Readers, As discussed in last article about deprecating of Business Central Administration, there are few common actions that we use in administration till Business Central 20. For our on-prem customers, we will still require doing activities. As Microsoft suggest we need to start using PowerShell cmdlet.    Let's see how to do those via PowerShell, or Administration Shell. I will be keep adding commands as you comment to this article.