Dynamics NAV 2017 Events

Dynamics NAV 2017 extensions

Well, I’m gonna try to explain some things about extensions and events in Dynamics NAV

First of all, forget your previous assumptions on how do you need to develop anything in NAV, forget to change tables, pages, codeunits. The new development paradigm resides in “create everything new and attach those new things to events”.

Yes, you need to use (purchase) objects.

What we are going to achieve with this new approach? A more stable Dynamics NAV, less problems with developments, an easy upgrade path and maybe, just maybe, some repeatability in the code deployments (this always depends on customer´s ability to understand a standard process).

There is no better way to learn than with a real example.

This is the scenario, in Mexico (with all its stupid’s laws) you need to withhold taxes from certain types of vendors usually called professional services, lease or freights.

This withholding has different types and percentages, for example, if the vendor is a person who gives you a receipt of fees for his work you need to make the next operations

Amount (+) 1000
VAT 16% (+) 160
VAT Withholding 2/3 of VAT (-) 10.67 106.67
Income Tax Withholding (-) 10% 100
Total Amount 953.33

Besides the calculation, you need to store the withholdings in a specific G/L account

Remember you cannot modify the standard tables, pages or codeunits on NAV!!!

My approach is:

Create a table called Withholding Groups

001

Create a “alternate” G/L Account table and in the new table, add some fields to the G/L Account

002

It’s very important to keep G/L Account and the new G/L Account copy synchronized

To achieve this first create a new codeunit (I called eAccSubscribers).

Create a new function called OnGLAccInsert (I used this kind of names to allow a very easy understanding).

003

Once created, click in the Properties button in the menú

004

Local must be No

Event – select Subscriber

EventPublisherObject – I use table 15, btw you can type “t 15” and NAV complete the field when you change to the next field

Event Function – choose OnAfterInsertEvent from the list

005

As you see, you have triggers or events for some actions or processes in NAV

In the next window asking for confirmation click ok.

Now let’s write some code, the subscriber has 2 parameters added by NAV, a Rec and a Boolean, the rec is related to the G/L account table so I´ve added a new variable to my G/L Account table called eAccGL.

The code is

eAccGL.”No.” := Rec.”No.”;

eAccGL.INSERT(TRUE)

What are we do to this moment? When a new G/L Account is inserted in the NAV Table 15, the code takes the t15.”No.” value and inserts the same data in the new table.

Now, you need to create another subscriber to complete the G/L Account information so we create a new function called OnGLAccModify with the next properties

006

Nav adds these parameters

007

We can use a TRANSFERFIELDS but I don’t need all G/L Account fields, just some, so I need to write every field one by one

008

and so on.

You can create an On Delete function.

Now save the codeunit and create a new G/L Account, then run the copied table and you´ll see the added account.

Now, you need to setup the G/L accounts and the withholding groups before start the tricky part, insert new lines before post execution code.

In previous versions you can create a codeunit to check if the G/L account needs to be withholded, and create the new lines in purchase line table and finally, you have some choices, create a button in the menu to call the create lines function (Page modification), add this behavior in the Release button (Codeunit Modification) or modify codeunit 90 (again codeunit modification).

Now we can create the same codeunit to check G/L Account, create lines, etc), but in this version we create a subscriber called CheckIFWHGLAccOnPost, the eventpublisherobject is the codeunit 90 and the event is OnBeforePostPurchaseDoc

009

Add some code to make all calculations and line insertions like this

010

Finally, test the function

Create Purchase Invoice and select the account with withholding

011

Post and check the lines

012

 

Anuncios