Thursday, March 24, 2022

D365FO Customize SSRS report using extension X++


 In this tutorial, will show you how to customize & extend data set and design of Free Text Invoice report. Let’s start.

I have to add two fields in the table and to show these fields in Free Text Invoice report. If you have come across the Free Text Invoice report. You may have noticed that it uses contract class, controller class, DP class and print management class to generate the report.

What you have to do to customize SSRS report using extension in D365FO is to create the extension of table, implement the events to insert the data into the extended fields, customize the design, extend the controller class and subscribe to the delegate of print management document type class to re-route the print management to execute your new design.

I have done the following steps:

  • Create extension of FreeTextInvoiceTmp table and add the field i.e. TotalAmounInWords
  • Create a class named FreeTextInvoiceController_Events to implement the event handler methods
  • Copy the post handler event method of processReport() of FreeTextInvoiceDP class and paste in the class created in previous step.
    • NOTE: You can also use the OnInserting event of table instead of processReport() of DP class.
  • Write the following logic to insert the data into the
    TotalAmounInWords field we have created:
class FreeTextInvoiceController_Events
{
    [PostHandlerFor(classStr(FreeTextInvoiceDP), methodStr(FreeTextInvoiceDP, processReport))]
    public static void FreeTextInvoiceDP_Post_processReport(XppPrePostArgs args)
    {
        FreeTextInvoiceDP dpInstance = args.getThis() as FreeTextInvoiceDP;
        FreeTextInvoiceHeaderFooterTmp freeTextInvoiceHeaderFooterTmp = dpInstance.getFreeTextInvoiceHeaderFooterTmp();
        FreeTextInvoiceTmp freeTextInvoiceTmp = dpInstance.getFreeTextInvoiceTmp();
        ttsbegin;
        while select forUpdate freeTextInvoiceTmp
        {
            freeTextInvoiceTmp.TotalAmounInWords = numeralsToTxt(freeTextInvoiceTmp.InvoiceAmount);
            freeTextInvoiceTmp.update();
        }
        ttscommit;
    }
}
  • Find out the FreeTextInvoice report in the AOT,
  • Right-click the report and click Duplicate in project.
  • Customize the design and add TotalAmountInWords field as per your requirements
  • Create another class named FreeTextInvoiceControllerExt which extends the FreeTextInvoiceController class. Override the main() method. Give the new report and it’s design name in it.
  • Add the following logic in FreeTextInvoiceControllerExt class:
class FreeTextInvoiceControllerExt extends FreeTextInvoiceController
{
    public static FreeTextInvoiceControllerExt construct()
    {
        return new FreeTextInvoiceControllerExt();
    }

    public static void main(Args _args)
    {
        SrsReportRunController formLetterController = FreeTextInvoiceControllerExt::construct();
        FreeTextInvoiceControllerExt controller = formLetterController;
        controller.parmReportName(ssrsReportStr(FreeTextInvoiceCopy, Report));
        controller.parmArgs(_args);
        controller.startOperation();
    }
}
  • Create another class named PrintMgtDocTypeHandlersExt
  • Add a method in it which subscribes to the event delegate of PrintMgmtDocType class to re-route the report from the default to this customized report
  • Add the following logic in PrintMgtDocTypeHandlersExt class:
class PrintMgtDocTypeHandlersExt
{
    [SubscribesTo(classstr(PrintMgmtDocType), delegatestr(PrintMgmtDocType, getDefaultReportFormatDelegate))]
    public static void getDefaultReportFormatDelegate(PrintMgmtDocumentType _docType, EventHandlerResult _result)
    {
        switch (_docType)
        {
            case PrintMgmtDocumentType::SalesFreeTextInvoice:
                _result.result(ssrsReportStr(FreeTextInvoiceCopy, Report));
                break;
        }
    }
}
  • Create extension of the following menu items which executes report:
    • FreeTextInvoiceCopy
    • FreeTextInvoiceOriginal
  • Change the controller class name in the Object property of the extended menu items to the FreeTextInvoiceControllerExt class created above
  • Save, build and synchronize and deploy the project/reports.

In this way, we can customize SSRS report using extension in D365FO


Also, see the article about how to Create SSRS report and how to Customize SSRS report using extension and D365 Running an SSRS report from the selected record.





No comments:

Post a Comment