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;
    }

How to post multiple Free Text Invoices using x++ D365 FO

These days one of the most common requirements is to post multiple Customer Invoices in a single click.


Here is an example :-

[FormControlEventHandler(formControlStr(CustFreeInvoice, BulkPost), FormControlEventType::Clicked)]
public static void BulkPost_OnClicked(FormControl sender, FormControlEventArgs e)
{

    FormDataSource                fds;
    MultiSelectionHelper         selectionHelper = MultiSelectionHelper::construct();
    Set                                       selectedRecords = new Set(Types::Record);
    CustInvoiceTable                custInvoiceTable;
    CustPostInvoice                  custPostInvoice;
     
        fds = sender.formRun().dataSource('CustInvoiceTable');

        custInvoiceTable = fds.cursor();

        selectionHelper.parmDataSource(fds);
        custInvoiceTable  = selectionHelper.getFirst();

        if (custInvoiceTable.RecId)
        {
            while (custInvoiceTable)
            {
                selectedRecords.add(custInvoiceTable);
                custPostInvoice = new CustPostInvoice(custInvoiceTable);
                custPostInvoice.run();
                custInvoiceTable = selectionHelper.getNext();
            }
        }

        fds.research();
        fds.reread();
        fds.refresh();

        Box::info('Invoice Posting Completed Successfully');
}

How to create a Dimension Value using x++ D365 FO

There are lot of tables holding values for Financial Dimensions such as DimensionAttributeValue and DimensionAttributeValueCombination but in case if we can't find the values in these above tables then we have another table known as DimensionFinancialTag through which we can get values as well as we can create new dimensions as well.


Here is an example mentioned below :-

        
 DimDetails                                          dimensiondetails;
 DimensionAttribute                             dimAttr;
 DimensionAttributeValue                    dimAttrValue;
 DimensionDefault                                result;
 DimensionFinancialTag                       dimFinTag , dimFinTagval;
 RecId                                                    dimFinancialCategoryRecid;
        
DimensionAttributeValueSetStorage   valueSetStorage = new 

DimensionAttributeValueSetStorage();

        try
        {
        dimAttr = DimensionAttribute::findByName('ABC');
        dimFinancialCategoryRecid = dimAttr.financialTagCategory();
        while select dimensiondetails where dimensiondetails.UpdateFlag==0
        {
            select dimFinTag where
                   dimFinTag.FinancialTagCategory == dimFinancialCategoryRecid &&
                   dimFinTag.Value == dimensiondetails.AssetCode;

            if(!dimFinTag.RecId)
            {
                dimFinTagval.Value = dimensiondetails.AssetCode;
                dimFinTagval.Description = dimensiondetails.AssetDescription;
                dimFinTagval.FinancialTagCategory = dimFinancialCategoryRecid;
                dimFinTagval.insert();
                
            }

            
        }

        }

        catch(Exception::Error)
        {
            ttsAbort;
            throw Exception::Error;
        }

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...