Monday, February 25, 2019

MSDYN365BC - Futuristic CAL Development - Customize Base Code 3.

Hi Readers,

As discussed in the Last article, in this post we will continue with our Customer Requirements.

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

In this article, we will discuss adding custom code in base objects and what is the Futuristic way of adding code.

In your customer are Pre-2018 then you can use Hooks for those instances. (Refer Old articles to check how to use Hooks).



Next Part of Requirement - 

  • Expense Code is mandatory for all lines during the Posting of the Purchase order and will also check and Expense Code is not Disabled.
  • Expense Code should be available in General Ledger Entries.

As all we are C/AL Experts, we will try to write code in Codeunit 90 for the first requirement. We will write code in OnRun Trigger Before Posting Lines Begin as shown below.


As you can see in the above image -

  • Old Way - is the old way to write custom code by modifying base codeunit 90.
  • New Way - is the New way to write custom code with an Event in Base code.

Now as we have done in previous articles we are supposed to subscribe OnBeforePostLines Event in our Expense Codeunit and then write our custom code.

So let's start that -

1. Design Codeunit that we developed for placing all custom code related to this customization and add a function to subscribe to base events, as shown below.

  • Function Name - OnBeforePurchaseOrderPostLines
  • Event - Subscriber
  • EventPublisherObject - Codeunit Purch.-Post


And Rest is code that we all know as shown below.



Let's Start with Part 2 of requirement - Expense Code should be available in General Ledger Entries for Purchase Lines with G/L Account.  

How Standard NAV pass a field value posted to G/L Entries from Purchase order Lines?

  1. Codeunit 90 - Will move data from Purchase Line to Invoice Post. Buffer.
  2. Codeunit 90 - Will move data from Invoice Post. Buffer to Gen. Journal Line.
  3. Codeunit 12 - Will move data from Gen. Journal Line to General Ledger Entries.

As we all understand that as field data need to move in Invoice Post. Buffer and then to Gen. Journal Line and then to General Ledger Entries.

Let's add Expense Code field above-Listed Listed tables to move data. And also add "Expense Code" Field in General Ledger Entries Page as shown below.



Now as we have done the background work lets work toward code in Futuristic Way.

Codeunit 90 - PreparePurchase Which allow us to use Event to use for moving data from Purchase Line to Invoice Post. Buffer.



Codeunit 90 - CopyFromInvoicePostBuffer which allow us to use Event to use for moving data from Invoice Post. Buffer to Gen. Journal Line.



Codeunit 12 - CopyGLEntryFromGenJnlLine which allow us to use Event for moving data from Gen. Journal Line to General Ledger Entries.



Let's subscribe to Events that we identifed in our Expense Subscriber Codeunit.

Add three Functions for three Subscribers function as shown below -

  1. ExpenseCodeOnAfterCopyGenJnlLineFromInvPostBuffer
  2. ExpenseCodeOnAfterInvPostBufferPreparePurchase
  3. ExpenseCodeOnAfterCopyGLEntryFromGenJnlLine



Now last part which is quite simple for NAV Experts. One line of code in all three subscribers, as shown below.


In Next Article, we will test our solution and correct code if required during testing.

Let us know your thoughts as a comment to article. Hope it make sense!

Regards,
Saurav Dhyani

2 comments: