Dynamics NAV 2016 Events
I have been coding in Dynamics NAV 2016 CU3 and have started to use the new events features in Dynamics NAV, I have watched a few videos and read a few articles, many have ‘Foo Bar’ and ‘Hello World!’, these are required reading or watching if you are new to events, I would suggest following the links on this website page, that’s where I started to get behind the events.
I will not be going into to much detail in this article, I was hoping to find a “Real World!” as well as a “Hello World!” example to look at, I was not lucky if there is one Google did not find it for me, I decided to spend my weekend putting an example together from scratch, I cannot share any code from my consultancy as it is not my IP, I have included two downloads at the end of this Page.
What I decided to build is something that most Resellers have developed at some point an interface to Web Orders, in the earlier versions to maintain the integrity of the Orders it could have meant changes to Sales Header, Sales Line, and Codeunit 80 Sales Post as well as Pages.
In this workflow in the standard objects I have added one “Page Action” to just three Pages, to show the power of events, I have not added a single line of code in any of the base objects, all the updating is done by the event calls in standard objects.
My Web Import
I needed somewhere to run my web Imports from so I added a Menu, Web Order List has all active Web Orders, Web Order Import runs an XML Port to populate the tables, Web Integration List allow different integrations and holds the “Customer No.”, as these are Web Orders and the returning Customers are small then there is no point in creating a Customer for each Web Order.
The Web Order List is a standard list page running the Web Order Header table, one Page Action to Make a Sales Order, a nice and simple Page List.
The web Order Card displays the Information from the Web Order Header and Web Order Lines, the imported Order has a Status of ‘Blank’ and many values that any web Import would have.
When the Make Order Action is Pressed a Sales Order is Created, the Header and Line Status is updated and in the Posting Section the Sales Order No. is Visible.
I mentioned at the start of the post that I have built in some data integrity, here I tried to change the Unit Price on a Linked Order this has been done with an event on the Sales Line without changing any code in the Table.
In my Custom Codeunit I have a number of CAL Global Functions, the message above was triggered by the OnSalesOrderLineUnitPriceModify, you will notice I have one to Stop the Sales Header and Sales Line from being Deleted.
In the function Properties I selected the Event = Subscriber, I entered T37 in the Object, OnBeforeValidateEvent in the EventFunction and I want this event called when the Unit Price is updated.
This code in the Custom Codeunit is run when the Unit Price is changed in the Sales Line Table, it looks to see if the Line is linked to a Web Order Line, if it is it checks that the Price is the same as the Price the Customer Paid, if it is not then it raises an Error, no Code written in the Sales Line Table.
In this example the Sales Order gets Shipped by Codeunit 80, The Web Order Status has been Updated to ‘Shipped’, codeunit 80 has not been Modified an Event call has updated the Web Order.
In my Custom Codeunit I have a Function SalesOrderPosted, in this one I entered C80 in the EventPublisherObject and I selected the OnAfterPostSalesDoc this Event runs after the Codeunit has done a Commit, I accepted the parameters.
These Parameters are passed from Codeunit 80, if the Order is completed and Invoiced then the Sales Header will no longer exist, so I use the passed in SalesHeader variable to find the Web Order Lines.
If the SalesShptHdrNo parameter has a value then One or More of the Order Lines have been shipped, in the function I match any Web Order Lines and update the Web Order Line Status, two blocks of code are used as the user might have Shipped and Invoiced different lines at different times.
If the SalesInvHdrNo parameter has a value then One or More of the Order Lines have been Invoiced, in the function I match any Web Order Lines and update the Web Order Line Status, the Web Order Header “Order Status” is updated from the Web Order Lines.
Standard Object Changes
In the Objects I created a Web Order Detail Table and Page, when the user Runs the action the Page shows the Additional Web Order Detail, I could have made a Sub Page and added it to the Sales Order Card but not all Orders are Web Orders and the only time this would be required is if there was an enquiry, this also can be seen from the Posted Sales Shipment and Posted Sales Invoice.
Here is the Page Action I added to the Sales Order Card, there is no code in the Sales Header, Sales Line or in the Page for this Action.
Action Properties.
Web Order Detail Functions, these are function I created to maintain the Web Order Detail, these Include actions when the Sales Order is Created or Deleted, Sales Shipment Is Created or Deleted, and when the Sales Invoice is Created or Deleted.
The last four functions run the Web Order Detail Page when the action button is pressed on the Document Pages.
Code to delete the Web Order Detail when the Sales Header is Deleted.
This code is called from the AfterSalesPost and Creates the Shipment Web Order Details.
When the Web Detail action is pressed on the Sales Invoice Page, the Page Action Event is triggered and the code below is run.
Web Order Objects and Tables
These are the Objects and Tables I created for this example, you can see that only three standard objects were modified, these tables are based on my experiences with Web Imports, quite often I see many field values Imported into large Import tables which have no use or meaning.
Web Order Header table:
Web Order Line:
Web Order Detail:
Conclusion
I have found that coding with Events is a Positive Change and should help cut down the Upgrade Path, if we look at this example I would only have to add the three ‘Page Actions’ and the Custom Range Objects to any other database that requires the Web Order Modules.
This Download has all the GB objects including the XML Files, it was created from 2016 CU3 and may not be compatible with later Versions, it is not complete code and is for personal use only.
This Download has the Custom objects only and includes the XML Files, you will need to add the three page actions as above, it was created from 2016 CU3 and may not be compatible with later Versions, it is not complete code and is for personal use only.
Thanks for reading this Article, I hope it can be of some benefit.