We in the EMEA Customer Support and Services team receive from time to time a request to implement the Copy Loop feature as it has been for RDLC and/or Classic Client reports. Basically, implementing a copy loop means giving the user the capability to choose one or more copies of a specific document to be printed together with the original one in one go.
In this blog, I am proposing a way to easily implement a Copy Loop using the Word Report Layout feature that was introduced in Microsoft Dynamics NAV 2015 and enhanced version after version.
What will follow and the objects attached (reports 1305 Standard Sales – Order Conf. and 1306 Standard Sales – Invoice) are based on Microsoft Dynamics NAV 2017 CU 2 W1. Modifications made could be easily applied also to previous versions (Microsoft Dynamics NAV 2016 and Microsoft Dynamics NAV 2015).
Below, I am considering modifications to Report 1306 Standard Sales – Invoice.
C/AL application code and report changes
- Add the following global variables:
Name DataType Subtype Length NoOfCopies Integer NoOfLoops Integer CopyDesc Text 30 OldHeader Record Sales Invoice Header I Integer RunningHeader Record Sales Invoice Header
- Add the following text constant:
Name ConstValue CopyDescLbl *** DOCUMENT COPY ***
- Add the following field into Request Page
SourceExpr Name Caption NoOfCopies NoOfCopies No. of Copies
- Add the following column into Header DataItem (see below lines or image)
Data Source Name CopyDesc CopyDesc
- Add an Integer DataItem called “CopyLoop” on top of all the others with indentation 0 and the following property set as per below
DataItemTableView SORTING(Number) ORDER(Ascending)
- Select ALL the other elements (DataItems + Columns) except “CopyLoop” and indent them all (see picture below)
- Change the report property WordMergeDataItem from “Header” to “CopyLoop”.
This is the crucial point that would instruct the application to create a brand new section and reset section page number within the Word document. Old C/AL sailors might notice that this DataItem is placed in a different position rather than where is located typically within RDLC or Classic Client reports.See the image below.
- Change the C/AL Code as per the following (check below code or image)
CopyLoop – OnPreDataItem()
NoOfLoops := Header.COUNT * (ABS(NoOfCopies) + 1); //MSDN-Blog CopyDesc := ''; //MSDN-Blog SETRANGE(Number,1,NoOfLoops); //MSDN-Blog FirstLineHasBeenOutput := FALSE; //MSDN-Blog OldHeader.COPY(Header); //MSDN-Blog RunningHeader.COPY(OldHeader); //MSDN-Blog
CopyLoop – OnAfterGetRecord()
IF RunningHeader.NEXT = 0 THEN BEGIN //MSDN-Blog RunningHeader.COPY(OldHeader); //MSDN-Blog RunningHeader.FINDFIRST; //MSDN-Blog CopyDesc := CopyDescLbl; //MSDN-Blog END; //MSDN-Blog
CopyLoop – OnPostDataItem()
Header – OnPreDataItem()
SETRANGE("No.",RunningHeader."No."); //MSDN-Blog //FirstLineHasBeenOutput := FALSE; //MSDN-Blog
Word Layout creation and basic changes
With the modification provided in the C/AL application code and report changes, it is now possible to prompt users to input the number of copies and have these printed out in one go. However, there are still a couple of small modifications that needs to be done in the Word Layout. These are mainly two:
- Add the variable CopyDesc to the Word layout, in order to distinguish between the original and the copies.
- Change the page numbering (page number / total pages) in order to cope with the report refactoring.
To finalize the implementation, then, here is what to do about the Word layout:
- In the Windows client, search for Custom Report Layout
- In the Custom Report Layout window, click the New action button
- Select Report ID 1306, choose the Insert Word Layout field, and then press OK
- Rename the new created record description from Copy of Built-in Layout to Sales – Invoice with CopyLoop
- Select the new record created and click the Edit Layout action button
- In the Word layout, right click in the header and select “Edit Header”
- Add CopyDesc as Plain Text from the Developer > XML Mapping Pane menu in the bottom of the very first rectangle and make it bold. See image below:
- Select the first Number right after Page_Lbl (that’s current page number), right click and select “Edit Field”. Select Field Names: Page and Format: 1,2,3, …
- Select the second Number right after Page_Lbl (that should be the total pages for the current document), right click and select “Edit Field”. Select Field Names: SectionPages and Format: 1,2,3, …
- Save, and close the Word layout, and import the changes back into NAV.
- Close the Custom Report Layout window
- Search for Report Layout Selection
- In the Report Layout Selection window, filter the list for Report ID 1306
- Change the Selected Layout field to Custom Layout and choose Report ID 1306 with the description Sales – Invoice with CopyLoop. Then click OK to close the page and confirm.
- Open the Report Selections Sales window, and then choose the usage Invoice
- Be sure to have Report 1306 added as per the following
Everything should be now good to go.
Give it a spin with Posted Documents > Posted Sales Invoices.
These postings are provided “AS IS” with no warranties and confer no rights. You assume all risk for your use.
Duilio Tacconi (dtacconi)
Microsoft Dynamics Italy
Microsoft Customer Service and Support (CSS) EMEA
Thanks to Roberto Stefanetti from SOLUZIONIEDP.IT to open this Advisory request to EMEA CSS!