Sunday, June 23, 2019

How to send multiple Free Text Invoices for direct printing to printer using x++ D365 FO

One of the typical requirements from most of the customers is to directly send multiple Posted Customer Invoices to the Network Printer so that we can save ourselves from the pain of Exporting to PDF files manually and printing them later.


Before we write the code to achieve this functionality. We need to follow few steps which are mentioned below :-


1) Go to Organization Administration --> Setup --> Network Printers --> download Microsoft

Dynamics Routing Agent (You can find this in action pane)

2) Once downloaded and installed open DRA

3) Go to settings and enter the D365 URL and Azure AD tenant :-
 
          D365 URL :- https://abc-aos.ax.dynamics.com

          Azure AD Tenant :- abcxyz.onmicrosoft.com

4) Do not select the option Run as Windows Service

5) Close the application and reopen it

6) Enter the credentials for the Azure AD Tenant

7)Click Printers and it will show all the printers available on your specific network

8) Mark your required printers and click Register


Once done write the below code in an event handler :-


 [FormControlEventHandler(formControlStr(CustFreeInvoice, BulkInvoicePrint), FormControlEventType::Clicked)]
    public static void BulkInvoicePrint_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        int                                             loop;
        FormDataSource                      fds;
        MultiSelectionHelper               selectionHelper = MultiSelectionHelper::construct();
        Set                                             selectedRecords = new Set(Types::Record);
        CustPrintOutInvoice                 custPrintOutInvoice;
        NoYes                                       usePrintManagement;
        RecordSortedList                      recordSortedList;
        CustInvoiceTable                      currentCustInvoiceTable;
        SalesInvoiceJournalPrint          salesInvoiceJournalPrint;
        CustInvoiceJour                        custInvoiceJour;
        CustInvoiceTable                      currCustInvoiceTable;
        SRSPrintDestinationSettings    srsPrintDestinationSettings;
        RecordSortedList                      custInvoiceJourList;
        RecordSortedList                      rsl;
        SrsReportRunController              controller = new SrsReportRunController();
        FreeTextInvoiceContract  rdpContract = new FreeTextInvoiceContract();
        SysCorpNetPrinters                      netPrinters;
     

        fds = sender.formRun().dataSource('CustInvoiceTable');

        rsl = FreeTextInvoiceEventHandler::getSelectedRecords(fds);

        Args args = new Args();

        select firstonly netPrinters where
        netPrinters.Active == NoYesCombo::Yes &&
            netPrinters.EnableSalesInvoiceDocumentPrint==NoYes::Yes;

        custInvoiceJourList = new RecordSortedList(tableNum(CustInvoiceJour));
        custInvoiceJourList.sortOrder(fieldNum(CustInvoiceJour, RecId));

        for (loop = rsl.len(); loop > 0; loop--)
        {
            rsl.next(currentCustInvoiceTable);
            custInvoiceJour = currentCustInvoiceTable.custInvoiceJour();
            if (custInvoiceJour.RecId)
            {
                selectedRecords.add(custInvoiceJour);
           
                controller.parmReportName(ssrsReportStr(FreeTextInvoiceNew, Report));
             
                controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);

                controller.parmShowDialog(false);

                rdpContract.parmRefRecid(currentCustInvoiceTable.RecId);
                rdpContract.parmInvoiceId(currentCustInvoiceTable.InvoiceId);

                controller.parmReportContract().parmRdpContract(rdpContract);

                srsPrintDestinationSettings = controller.parmReportContract().parmPrintSettings();
             
                srsPrintDestinationSettings.fromPage(1);
                srsPrintDestinationSettings.toPage(1);
                srsPrintDestinationSettings.landscape(false);
                srsPrintDestinationSettings.printerName(netPrinters.PrinterName);
                srsPrintDestinationSettings.printMediumType(SRSPrintMediumType::Printer);
                srsPrintDestinationSettings.numberOfCopies(1);
                srsPrintDestinationSettings.collate(false);
                controller.startOperation();
             
                stInvoiceJourList.ins(custInvoiceJour);
            }
         
        }

     



    }

 

     public static  RecordSortedList getSelectedRecords(FormDataSource fds1)
    {
        RecordSortedList recordSortedList;
        CustInvoiceTable currentCustInvoiceTable;

        recordSortedList = new RecordSortedList(tableNum(CustInvoiceTable));
        recordSortedList.sortOrder(fieldNum(CustInvoiceTable,RecId));

        currentCustInvoiceTable = Global::getFirstSelection(fds1);

        while (currentCustInvoiceTable)
        {
            recordSortedList.ins(currentCustInvoiceTable);
            currentCustInvoiceTable = fds1.getNext() as CustInvoiceTable;
        }

        return recordSortedList;
    }

No comments:

Post a Comment

How to reverse Free Text Invoice Voucher entries without Dialog

 Hey Folks ,  This blog post is in continuation of the previous post for Reversing Free Text Invoice Voucher entries with Dialog. Only diffe...