Subscribe via RSS Feed Connect on LinkedIn

Dynamics NAV 2016 Events

6th March 2016 0 Comments

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.

AWT-20160306152749

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.

AWT-20160306152943

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.

AWT-20160306153128

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.

AWT-20160306153357

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.

AWT-20160306153646

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.

AWT-20160306154310

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.

AWT-20160306154430

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.

AWT-20160306164512

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.

AWT-20160306153906

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.

AWT-20160306154727

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.

AWT-20160306155026

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.

AWT-20160306154911

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.

AWT-20160306154941

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.

AWT-20160306153820

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.

AWT-20160306150507

Action Properties.

AWT-20160306150536

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.

AWT-20160306155150

Code to delete the Web Order Detail when the Sales Header is Deleted.

AWT-20160306155304

This code is called from the AfterSalesPost and Creates the Shipment Web Order Details.

AWT-20160306155341

When the Web Detail action is pressed on the Sales Invoice Page, the Page Action Event is triggered and the code below is run.

AWT-20160306155432

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.

AWT-20160306150231

Web Order Header table:

AWT-20160306150323

Web Order Line:

AWT-20160306150343

Web Order Detail:

AWT-20160306150403

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.

Download “Web Order for Example 2016 CU3 GB Objects” Web-Import-2016-CU3-GB.zip – Downloaded 107 times – 211 KB

 

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.

Download “Web Order for Example 2016 CU3 WW Objects” Web-Import-2016-CU3-WW.zip – Downloaded 68 times – 147 KB

 

Thanks for reading this Article, I hope it can be of some benefit.

Please Share this Post:

Leave a Reply

Spam protection by WP Captcha-Free