Wednesday, July 26, 2023

D365 FO - New document type in Print Management setup Using X++

Print management is the framework that we use in Dynamics 365 Finance and Operations S to configure print settings for various business documents (SSRS Reports), including sending emails, archiving, saving as a file, printing automatically, or just displaying at the screen. Print management setups are typically separated by module and for each module, there are different nodes for all business documents that are available to be printed.
How to add a document type to the print management configuration in Dynamics 365 Finance.{Accounts Receivable Module}
Sometimes you need to create new business documents in Dynamics 365 F&O. So you need to add this business document/report in the Print management form setup.
To add this new document in Print Management you should follow those steps:
Step 1: 
The first step should be to extend the base enum print management document type [PrintMgmtDocumentType] and add a new element there.


In your new extension, add a new enum value, name it and give it a proper label.


Extend the node for your document type

There are already so called node-classes for various standard types and you just need to find the right one for your target module. In this example: Sales.


Step 2: 

The second step should be to extend the PrintMgmtNode class based on the module where you want to add the custom document. We will add a document to the accounts receivable print management configuration as an example.

Code:

/// <summary>/

/// Get Document type that we created

/// </summary>

[ExtensionOf(classStr(PrintMgmtNode_Sales))]

final class PrintMgmtNode_Sales_Extension

{

    public List getDocumentTypes()

    {

        List docuTypes;

         docuTypes = new List(Types::Enum);

         docuTypes = next getDocumentTypes();

         docuTypes.addEnd(PrintMgmtDocumentType::CustomDocument);

         return docuTypes;

    }

}

Step 3: For this business document you should add a default report in the Printer Management setup. To add a default report we should subscribe to the NotifyPopulate method of PrintMgmtReportFormatPublisher class and add the following code.

Code:

/// <summary>/

/// Get Document type that we created

/// </summary>

public class PrintMgmtReportFormatPublisherHelper

{

 /// <summary>/;

    ///  Insert a default report to custom document

    /// </summary>

   [SubscribesTo(classstr(PrintMgmtReportFormatPublisher), 
                 delegatestr(PrintMgmtReportFormatPublisher, notifyPopulate))]
    public static void notifyPopulate()
    {
        #PrintMgmtSetup
   void addFormat(PrintMgmtDocumentType _type, PrintMgmtReportFormatName _name, 
                   PrintMgmtReportFormatCountryRegionId _countryRegionId = #NoCountryRegionId)
        {
            PrintMgmtReportFormatPublisherHelper::addPrintMgmtReportFormat(_type, _name, 
                                                                            _name, 
               _countryRegionId);
        }
         addFormat(PrintMgmtDocumentType::CustomDocument, 
                      ssrsReportStr(CustomReport, Report));
    }
private static void addPrintMgmtReportFormat(
        PrintMgmtDocumentType _type,
        PrintMgmtReportFormatName _name,
        PrintMgmtReportFormatDescription _description,
        PrintMgmtReportFormatCountryRegionId _countryRegionId,
        PrintMgmtReportFormatSystem _system = false,
        PrintMgmtSSRS _ssrs = PrintMgmtSSRS::SSRS)
    {
        PrintMgmtReportFormat printMgmtReportFormat;
 
        select firstonly printMgmtReportFormat
            where printMgmtReportFormat.DocumentType == _type
                && printMgmtReportFormat.Description == _description
                && printMgmtReportFormat.CountryRegionId == _countryRegionId;
 
        if (!printMgmtReportFormat)
        {
            // Add the new format
            printMgmtReportFormat.clear();
            printMgmtReportFormat.DocumentType = _type;
            printMgmtReportFormat.Name = _name;
            printMgmtReportFormat.Description = _description;
            printMgmtReportFormat.CountryRegionId = _countryRegionId;
            printMgmtReportFormat.System = _system;
            printMgmtReportFormat.ssrs = _ssrs;
            printMgmtReportFormat.insert();
        }
    }
}

 Step 4: To get the default report, subscribe the delegate getDefaultReportFormatDelegate and getQueryTableIdDelegate of class PrintMgmtDocType.

Code:

/// <summary>/;
/// Associate new print management document type with a default report design
/// </summary>
Public class PrintMgmtDocTypeEventHandler
{
    [SubscribesTo(classStr(PrintMgmtDocType), delegateStr(PrintMgmtDocType, 
                                              getDefaultReportFormatDelegate))]
    public static void getDefaultReportFormat(PrintMgmtDocumentType _docType, 
                                                    EventHandlerResult _result)
    {
        switch (_docType)
        {
                case PrintMgmtDocumentType::CustomDocument:
                _result.result(ssrsReportStr(CustomReport, Report));
                break;
        }
    }
[SubscribesTo(classStr(PrintMgmtDocType), delegateStr(PrintMgmtDocType, 
                                                getQueryTableIdDelegate))]
Public static void PrintMgmtDocType_getQueryTableIdDelegate(PrintMgmtDocumentType _docType, 
                                                    EventHandlerResult _result)
    {
        TableId tableId;
             switch (_docType)
        {
            case PrintMgmtDocumentType::CustomDocument:
                tableId = tableNum(CustomReportTemp);
                _result.result(tableId);
                break;
        }
    }
 }

No comments:

Post a Comment