Sales Document Preview

As promised, it is time to share an enhancement for the document posting preview.

The posting preview functionality in NAV 2016 is built around a single-instance Codeunit number 19.  That Codeunit has temporary tables declared as global variables and what ever is written to these tables remains there even if the actual database write is rollbacked.

The standard functionality displays a navigation page where the user can view the entries about to be posted.

Preview

So far so good.

Still, my customers have never asked for this feature.  They have, however, asked for the possibility to preview the document report.  That is what this blog post is about.

PreviewDocument

There are a few steps we need to follow in order to get there.  I will go through these steps.  If you want to cheat than just download the attached objects for NAV 2016 W1 CU2 and merge the changes into your database.

We will use the same method, that is, saving the document header and document lines in temporary tables and using them to feed the reports.  We will start by creating a copy of our sales invoice (206) and sales credit memo reports (207).  We need a new reports to handle temporary tables and we also want to make a few changes to make sure that it is clearly a preview report.

The changes we make are;

  • Change the Text Constant “Sales – Invoice %1” to “Sales – Invoice Preview”
  • In the Request Page, remove options for NoOfCopies and LogInteraction
  • Change DataItem “Sales Invoice Header” and “Sales Invoice Line” to be temporary
  • Clear ReqFilterHeadingML and ReqFilterFields properties for DataItem “Sales Invoice Header”
  • Add a public function “SetProperties” with parameters for the temporary tables and use the COPY function to copy the temporary table instance to the report Data Items.

SetProperties

Make the same changes to the credit memo report.

Now, lets look at Codeunit 19 and add what ever we need there.  It should be obvious that we need to add the global variables for our sales document tables.  We also add a boolean variable to change the mode from navigation to sales document preview.

Codeunti19Globals.PNG

Make sure that these temporary tables are emptied in initialization.

DeleteAllTempTables.PNG

Use the new EventSubscriber functionality to gather the document header and lines.

EventSubscribers

Externally we should be able to change the preview mode to a document preview mode.

SetDocumentPreviewMode.PNG

The last function we need is the one running the preview report.  We define our two new reports as a local variable and call them from this function.

RunReportPreview.PNG

The final step for Codeunit 19 is to make sure that the correct action is taken depending on our new boolean variable.

ShowAllEntries.PNG

We also need to make changes to Codeunit 81.  From there the Preview is started.  We need to make a change to the standard code to enable the document preview.  The rollback must be performed before calling the ShowAllEntries.  Rollback is done by using ASSERTERROR ERROR(”);

ErrorMoved.PNG

Almost done.  Another public function to Codeunit 81 is needed.  Here we change the preview mode before starting the default preview functionality.

StartPreviewDocument.PNG

Now, we can start adding actions to the sales pages.  For example in page 9305, Sales Order List.

  • Copy action 25
  • Rename to “Preview Document”
  • Change Image to PrintDocument
  • Add Codeunit 81 as a local variable in the action C/AL code
  • Add C/AL code SalesPostYesNo.PreviewDocument(Rec);

Pressing that new action will bring up this request page.

RequestPage.PNG

Pressing Preview should give you a good preview of how the printed document will look like after posting.

PreviewSalesInvoice.PNG

Download DocumentPreview and start playing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.