Copy Document with Default Values

This presentation discusses one of those little nice-to-have Microsoft Dynamics NAV modifications. A 10-mouse-click operation is reduced to 2 mouse clicks. For repetitive daily operations like copying documents, this can lead to significant time savings.

Reading on you will get a feeling about the level of complexity of a small software modification. You may want to dare your own Microsoft Dynamics NAV modification.

Suppose you entered a new purchase invoice for WoodMart Supply Co. You want to use the Copy Documentfunction to fill this invoice with the values of the last invoice you posted for WoodMart.

Microsoft Dynamics NAV - Purchase Invoice
Microsoft Dynamics NAV – Purchase Invoice

When you click on the Copy Document icon the Copy Purchase Document window opens, but it defaults to the the Document Type Quote. You have to select the Document Type Posted Invoice first.

Microsoft Dynamics NAV - Copy Purchase Document
Microsoft Dynamics NAV – Copy Purchase Document

Next you drill down on the Document No., and a list of all posted purchase invoices for all vendors shows as seen below.

Now you have to find WoodMart again, and you have to be sure the invoice you are looking at is indeed the last WoodMart invoice, the one you wanted to copy.

Microsoft Dynamics NAV - Posted Purchase Invoices
Microsoft Dynamics NAV – Posted Purchase Invoices

t would be nice if the system would show only WoodMart invoices to begin with.

Design

I will now show you how to make a software modification for a more user-friendly Copy Document function. The first step is to find out where in the system you need to make the modification. One way to find out is this:

Microsoft Dynamics NAV - Copy Purchase Document with highlighted icon
Microsoft Dynamics NAV – Copy Purchase Document

Click on the icon of the three sails in the top left of the Copy Purchase Document window. A menu opens. Select

Help > About This Page

The About This Page window opens:

Microsoft Dynamics NAV - Copy Purchase Document - About This Page
Microsoft Dynamics NAV – Copy Purchase Document – About This Page

In the Page Information section you can read:

Page:          Copy Purchase Document (492)

Page Type: ReportProcessingOnly

The Page Type ReportProcessingOnly means that what you are looking at is not the object Page 492, rather it is the Request Form belonging to Report 492.

On your Windows Desktop start the Program 

Microsoft Dynamics NAV 2013 Development Environment.

Click on Tools > Object Designer

Find Report 492 Copy Purchase Document:

Microsoft Dynamics NAV - Object Designer - Report 492 Copy Purchase Document
Microsoft Dynamics NAV – Object Designer – Report 492 Copy Purchase Document

Click on Design. The Report 492 Copy Purchase Document – Report Designer window opens:

Microsoft Dynamics NAV - Report 492 Copy Purchase Document - Report Designer
Microsoft Dynamics NAV – Report 492 Copy Purchase Document – Report Designer

Click on View > Request Page. The Report 492 Copy Purchase Document – Request Options Page Designer window opens:

Microsoft Dynamics NAV - Report 492 Copy Purchase Document - Request Options Page Designer
Microsoft Dynamics NAV – Report 492 Copy Purchase Document – Request Options Page Designer

Click into the first blank line at the end of the list of controls (red arrow). Click on View > C/AL Code or press F9. The Report – C/AL Editor window opens:

Microsoft Dynamics NAV - Report 492 Copy Purchase Document - C/AL Editor
Microsoft Dynamics NAV – Report 492 Copy Purchase Document – C/AL Editor

You find a trigger called OnOpenPage(). This is the trigger that fires when the Report Request Page opens, and this is the place where you want to change the default behavior of the Page.

Before you begin, write a dated comment into the Documentation() trigger, briefly explaining your modification to any developer who might touch this object after you, even if it is yourself.

Copy and paste the following code into the OnOpenPage() trigger, right above the first line of standard code.

CASE PurchHeader."Document Type" OF
  PurchHeader."Document Type"::Order: DocType := DocType::"Posted Invoice";
  PurchHeader."Document Type"::Invoice: DocType := DocType::"Posted Invoice";
  PurchHeader."Document Type"::"Credit Memo": DocType := DocType::"Posted Credit Memo";
END;

Mark the beginning and ending of your code with your own abbreviations after two slashes, like //<PC021>. This way you and any developer coming after you will know this is not original Microsoft code. This is a great help when faced with future software development tasks or software upgrades.

Microsoft Dynamics NAV - Report 492 Copy Purchase Document - Documentation Trigger
Microsoft Dynamics NAV – Report 492 Copy Purchase Document – Documentation Trigger

What you have accomplished so far are the default values for the Document Type. If, for example, the user clicks the Copy Document function on a purchase invoice or order, the Document Type to be copied from defaults to Posted Invoice.

The code you just added addresses the most common purchase documents: orders, invoices, and credit memos. If you would like to have default values for other documents, such as quotes, blanket orders, or return orders, you can add lines of code according to your business rules.

The last task is to limit the lookup list of posted purchase invoices to WoodMart.

Save all your changes and open Report 492 again in Design mode.

Click View > C/AL Code or press F9.

The Report – C/AL Editor window opens.

Make a text search for LookupDocNo or scroll down until you find a trigger LookupDocNo().

Microsoft Dynamics NAV - Report 492 Copy Purchase Document - Lookup Document No.
Microsoft Dynamics NAV – Report 492 Copy Purchase Document – Lookup Document No.

This is the trigger that fires when a user clicks the drill down arrow in field Documment No.: in the Copy Purchase Document Page. Scroll further down until you find the line

DocType::”Posted Invoice”:

Microsoft Dynamics NAV - Report 492 Copy Purchase Document - C/AL Editor with custom code
Microsoft Dynamics NAV – Report 492 Copy Purchase Document – C/AL Editor with custom code

Just above the line

IF FORM.RUNMODAL(0,FromPurchInvHeader) = ACTION::LookupOK THEN

copy and paste the following code:

IF PurchHeader."Buy-from Vendor No." <> '' THEN BEGIN
  FromPurchInvHeader.SETCURRENTKEY("Buy-from Vendor No.");
  FromPurchInvHeader.SETRANGE("Buy-from Vendor No.",PurchHeader."Buy-      from Vendor No.");
END;

Again, mark the beginning and ending of your code with your own abbreviations after two slashes, like //<PC021>.

The code you just added applies to purchase invoices. Write similar code for the sections of posted credit memos and any other document type you want to cover.

Result

With all the software modificaitons made you are now back in the new WoodMart invoice, and you click Copy Document again.

Microsoft Dynamics NAV - Purchase Invoice
Microsoft Dynamics NAV – Purchase Invoice

The Copy Purchase Document window now defaults to the Document Type Posted Invoice.

Microsoft Dynamics NAV - Copy Purchase Document with default value
Microsoft Dynamics NAV – Copy Purchase Document with default value

When you click the drill down in field Document No.: the list of Posted Purchase Invoices is filtered on WoodMart.

Microsoft Dynamics NAV - Posted Purchase Invoices, filtered
Microsoft Dynamics NAV – Posted Purchase Invoices, filtered

If you want to copy the last WoodMart invoice, you only have to scroll to the bottom of the list and click OK.

Author: Thomas Paulsen

Published: 05-May-2014