Skip to main content

How to implement a Copy Loop in Word report layouts

How to implement a Copy Loop in Word report layouts

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

  1. 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
  2. Add the following text constant:
    Name ConstValue
    CopyDescLbl *** DOCUMENT COPY ***
  3. Add the following field into Request Page
    SourceExpr Name Caption
    NoOfCopies NoOfCopies No. of Copies
  4. Add the following column into Header DataItem (see below lines or image)
    Data Source Name
    CopyDesc CopyDesc
    navblog_copyloop1
  5. 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)
  6. Select ALL the other elements (DataItems + Columns) except “CopyLoop” and indent them all (see picture below)
    navblog_copyloop2
  7. 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.
    navblog_copyloop3
  8. 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
navblog_copyloop4

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:
  1. Add the variable CopyDesc to the Word layout, in order to distinguish between the original and the copies.
  2. 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:
  1. In the Windows client, search for Custom Report Layout
  2. In the Custom Report Layout window, click the New action button
  3. Select Report ID 1306, choose the Insert Word Layout field, and then press OK
  4. Rename the new created record description from Copy of Built-in Layout to Sales – Invoice with CopyLoop
  5. Select the new record created and click the Edit Layout action button
  6. In the Word layout, right click in the header and select “Edit Header”
  7. 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:
    navblog_copyloop5
  8. 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, …
    navblog_copyloop6
  9. 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, …
    navblog_copyloop7
  10. Save, and close the Word layout, and import the changes back into NAV.
  11. Close the Custom Report Layout window
  12. Search for Report Layout Selection
  13. In the Report Layout Selection window, filter the list for Report ID 1306
  14. 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.
    navblog_copyloop8
  15. Open the Report Selections Sales window, and then choose the usage Invoice
  16. Be sure to have Report 1306 added as per the following
    navblog_copyloop9


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!

Comments

Popular posts from this blog

Quick Fix Visual Studio displaying NAV Report Layout as XML

I got this issue with Visual Studio 2015 Enterprise Edition.  Every time I open a Dynamics NAV report layout in Visual Studio, RDLC is rendered as XML file instead of visual layout with all the fields and tool boxes.  How the report layout looks:  FIX : In order to fix the issue you need to install  "Microsoft SQL Server Data Tools" .  To install "Microsoft SQL Server Data Tools" you need to go to  Control Panel   ->  Programs and Features  - >  Microsoft Visual Studio <Edition>  -> Change Or you can re run the installation setup.  From the features list, enable "Microsoft SQL Server Data Tools" option and proceed.  Once the installation is completed you are good to go. This time report layout will open in correct design layout.  Hope this helps you to resolve the issue. happy designing.  Please provide your feedback with a comment.  Thank you and Regards, +256775120005 Kairo Okello Dan ERP Solutions Consultant

The Difference Between Microsoft Dynamics NAV and AX

The Difference Between Microsoft Dynamics NAV and AX Microsoft Dynamics offers a variety of ERP solutions. You might say there is a solution for every purpose and every industry. Two of the offerings,  Dynamics NAV  and  Dynamics AX (now part of  Dynamics 365 ) share some of the same features so you might wonder: ‘what are the differences and which is right for my organization?’ Both ERP solutions are highly customizable, available in and adaptable to many languages and currencies used around the world. The two solutions can also be employed in a central location or across diverse satellite locations and are designed to grow as a business grows. Their industry-specific functionality can assure compliance with local and regional requirements. In short, both solutions could rightly be described as comprehensive and well equipped to manage complex requirements of supply chain and inventory. So, what is the difference? Is one better than the other for your particular organiz

How to install a Microsoft Dynamics NAV cumulative update

How to install a Microsoft Dynamics NAV 2018 cumulative update Introduction This article describes how to install a cumulative update for Microsoft Dynamics NAV 2018. You have to recompile all objects after you install a cumulative update. A cumulative update is a cumulative set of files that includes all hotfixes and regulatory features that have been released for Microsoft Dynamics NAV 2018. A cumulative update includes files that are separated into the following folders: APPLICATION DVD See  Upgrading the Data to Microsoft Dynamics NAV 2018  for details. How to install the cumulative update files APPLICATION The APPLICATION folder includes the following files: AccumulatedChangeLog.<Locale>.<Build No.>.txt Changelog.<Locale>.<Build No.>.txt CUObjects.<Locale>.<Build No.>.fob Objects.<Locale>.Objects.<Locale>.<Build No.>.txt To install the application files, follow these