×

    By submitting this form, you are agreeing to Folio3's Privacy Policy and Terms of Service.

    Get a free Consultation

    Event Subscription is a powerful way of handling or changing built-in functionality of any extension in Business Central, that extension could be Microsoft extension or any third party extension. Before subscribing to any event we should make sure that event is published in a specific object (codeunit, pages etc.) of another extension.

    So in short, today we will be using a business central built-in scenario to demonstrate event subscription and how it comes handy in such scenarios. So initially we will create a credit memo from the posted sales invoice from the business central frontend then we will do the same thing in our extension using AL. Then we will be subscribing to relevant events and will see how subscription can help us to handle built-in functionality of business central. So let’s get started.

    1) Create Credit Memo using frontend.
    2) Create Credit Memo using AL (our own Codeunit).
    3) Create a Subscription Event using AL.

    Create Credit Memo using frontend:

    As shown in above screen shot we will open Correct from menu and click Create Corrective Credit Memo, if invoice is fully opened then at the backend Business Central will run CreateCreditMemoCopyDocument function from Codeunit “Correct Posted Sales Invoice” (1303).
    If invoice is not fully opened then at the backend Business Central will run CreateCorrectiveCreditMemo function from Codeunit “Correct Posted Sales Invoice” (1303).

    Code snippet for CreateCreditMemoCopyDocument  function is attached for reference:

    Code snippet for CreateCorrectiveCreditMemo function is attached for reference:

    After looking into both functions we can see one main distinction that CreateCorrectiveCreditMemo function has OnCreateCorrectiveCreditMemoOnBeforePageRun event, this is how business central publish/raise events so that another extension can modify its functionality by subscribing to this event if required. In the event parameter we can see first parameter is SalesHeader which is a header of newly created credit memo and second parameter is IsHandled which is Boolean and passed by reference, this Boolean variable is really important in a way while subscribing to event if we don’t want Business Central to run next piece of code then we can just set IsHandled to true during subscription.
    Next we will create a credit memo from our extension using AL.

    Create Credit Memo using AL:

    Now we will achieve the same functionality of creating credit memo from Posted Invoice using our own Codeunit in our custom extension.

    Code snippet for our Custom CreditMemo function is attached:

    As we can see our custom code call the same CreateCorrectiveCreditMemo function which is also called by Business Central frontend while creating credit memo, now what we want to do is to change the behavior of this function (CreateCorrectiveCreditMemo) depending on the fact if it is called by our own custom function or by standard Business Central function, in order to do that what we will do is to create an entry of specific invoice in our custom F3ComAXSalesInvoiceMapping table after creating credit memo using our custom function, this custom table will help us to keep track of all the invoices we have worked on from our custom function.
    The above function CustomCreateCreditMemo takes Posted Invoice No. as an argument and creates a credit memo based on the fact that a specific invoice is fully open or not. When invoice is not fully open then CreateCorrectiveCreditMemo function is going to get called and as we saw earlier after creating the credit memo this function is opening the same credit memo in the form of Page after it’s being created, so basically whenever our custom function is calling CreateCorrectiveCreditMemo function that’s when we don’t want to open credit memo page. So in order to achieve that Event Subscription comes in handy, we will be subscribing to OnCreateCorrectiveCreditMemoOnBeforePageRun Event which is raised in CreateCorrectiveCreditMemo function.

    Create Subscription Event using AL:

    Code snipped for event subscription:

    In the above screenshot we have implemented a function which has some code in it. What that code does is basically look into the Custom F3ComAXSalesInvoiceMapping table to get the entry on the basis of Applies-to Doc. No. field, this field contain the invoice number from which we created a credit memo, if entry found that means this credit memo is created by our custom code, in that case we will set IsHandled flag to true which means Business central will not run the next piece of code (Credit Memo Page will not open), if entry not found that means credit memo is created by standard Business Central code and remaining code will be executed and credit memo page will be opened.
    Above function will be executed whenever OnCreateCorrectiveCreditMemoOnBeforePageRun Event is being raised. So how did we link our subscriber function to that event? We defined this on function header, EventSubscriber keyword is showing that this function will be worked as Subscriber, ObjectType would be Codeunit in our case and name of Codeunit would be “Correct Posted Sales Invoice” because the event we want to subscribe is published in “Correct Posted Sales Invoice” Codeunit, then we will provide event name on function header and next two parameters would be false.

    Summary:
    In summary what we have done so far is to change the behavior of standard Business Central function
    By not changing the standard code itself but subscribing to relevant events, like that we can subscribe to any event which is raised in any extension and achieve our desired functionality.