In the standard D365FO print management form, we have a number of predefined document types which can be setup to print different reports, however it is not an uncommon requirement to create one or multiple custom electronic reporting CBD formats for a document which does not exist in print management.
In this post we are going to see how we can add a new document type to print management that we can use to configure and run custom CBD formats.
Adding a new document type
Firstly, we are going to start off by adding our document type to the print management form of an existing or a custom module.
The steps we need to take are as follows:
- We extend the PrintMgmtDocumentType enum:
- Add a new element with a relevant name for your report.
- Create an extension of the PrintMgmtNode class specific to your module. If for example, we are working with a standard module, we are going to modify the WhsPrintMgmtNode_WHS class which applies to the Warehouse management module. Modify the getDocumentTypes method of the class and add our document type to this print management node. We can do this with the following code:
- Add a new element with a relevant name for your report.
public List getDocumentTypes()
{
List docTypes = next getDocumentTypes();
docTypes.addEnd(PrintMgmtDocumentType::CustomDocument);
return docTypes;
}
Associate our CBD report with a document type
After finishing the above steps, we will be able to see our custom document type added to the specific module’s print management screen. However, we will still not able to see our custom CBD format in the Report format field in print management.
To achieve this, our next step would be to extend the PrintMgmtReportFormatPopulator class, which will allow us to associate our custom CBD formats to our newly created document type. For this purpose, we can use the addOther method which takes 4 arguments:
- PrintMgmtDocumentType enum – we send our newly added document type
- CBD format name
- CBD format description
- CBD format country/region code
[ExtensionOf(classStr(PrintMgmtReportFormatPopulator))]
final class PrintMgmtReportFormatPopulator_Extension
{
protected void addDocuments()
{
this.addOther(PrintMgmtDocumentType::CustomDocument, 'Custom format name’, 'Custom format
description', 'Custom format country code');
next addDocuments();
}
}
Fetching our CBD format
Lastly, we need to get the CBD format which is selected for our document type in the print management setup.
public static ERFormatMappingId getFormatMapping()
{
PrintMgmtDocInstance printMgmtDocInstance;
PrintMgmtReportFormat printMgmtReportFormat;
PrintMgmtSettings printMgmtSettings;
ERFormatMappingTable erFormatMappingTable;
DataAreaId currentCompany = curExt();
select firstonly RecId from printMgmtDocInstance
where printMgmtDocInstance.DocumentType == PrintMgmtDocumentType::CustomDocument
&& printMgmtDocInstance.PrintType == PrintMgmtDocInstanceType::Original
&& printMgmtDocInstance.DataAreaId == currentCompany;
select firstonly Name from printMgmtReportFormat
where printMgmtReportFormat.DocumentType == PrintMgmtDocumentType::CustomDocument
exists join printMgmtSettings
where printMgmtSettings.ReportFormat == printMgmtReportFormat.RecId
&& printMgmtSettings.ParentId == printMgmtDocInstance.RecId
&& printMgmtSettings.DataAreaId == currentCompany;
select firstonly RecId from erFormatMappingTable
where erFormatMappingTable.Guid ==
str2Guid(subStr(printMgmtReportFormat.Name, 21, strLen(printMgmtReportFormat.Name)-1));
return erFormatMappingTable.RecId;
}
The above method will return the format mapping object of the CBD format that is set up for our document type. The returned RecId can now be used with the following code to create a format mapping object:
ERObjectsFactory::createFormatMappingRunByFormatMappingId(formatMappingId);
These methods can be now used in your electronic reporting classes (service/controller/contract) to run the report.
Final thoughts
After following the steps above steps, we have the framework setup to use a custom CBD with print management:
In case we have a custom module where we want to add a new report to print management, we will have to extend the PrintMgmtNode class and implement its methods. This will be described in more details in a future post.