In most of the multi-company projects on which I have worked on, I was asked to created separate layouts for documents like Purchase order or Sales invoice
for each Dynamics AX company. One of the approaches was to create
several Dynamics AX report designs and switch them by calling the report
member method design() depending on
the company the user is. But this method proved to be too complicated,
especially when the report logic is different for each company and it is
much more difficult to control changes if the same report is used in
more than two companies.
Another more convenient
approach, which I used for a number of implementations, was to use a
separate report for each company and switch them depending on in which
company it is being printed.
In this recipe, we will demonstrate the latter technique to print different reports per company. As an example we will use a Purchase Order document.
How to do it...
1. In AOT make a duplicate of the PurchPurchaseOrder report. Name it PurchPurchaseOrder2 and change its purchaseOrderTxt() method found in the PurchaseOrder section to:
display str purchaseOrderTxt()
{
return "PurchPurchaseOrder2";
}
2. In AOT, create a new class called PurchPurchaseOrder with the following code (replace CEU with one of your company codes):
class PurchPurchaseOrder
{
}
public static void main(Args _args)
{
ReportRun reportRun;
ReportName reportName;
;
switch (curext())
{
case 'CEU':
reportName = reportstr(PurchPurchaseOrder2);
break;
default:
reportName = reportstr(PurchPurchaseOrder);
break;
company specific document layoutcompany specific document layoutcreating, steps}
_args.name(reportName);
reportRun = new ReportRun(_args);
reportRun.init();
reportRun.run();
}
3. Modify the properties of the PurchPurchaseOrder, PurchPurchaseOrderCopy, and PurchPurchaseOrderOriginal
Output menu items as following:
Property
|
Value
|
---|
ObjectType
|
Class
|
Object
|
PurchPurchaseOrder
|
4. To test, open Accounts payable | Purchase Order Details, select any open purchase order and post it using the Post | Purchase order button. If the current company matches the company defined earlier, i.e. CEU, the report title should look like the following:
How it works...
In this recipe, first we
make a copy of the existing purchase order report and change its title.
This is done only to make sure we can distinguish which report is
printed. Normally, PurchPurchaseOrder2 should be a valid report for one of the companies.
PurchPurchaseOrder
is the main place where report switching is done. It is like a thin
layer between the report caller and the report itself. All the code is
placed in its main(). Here we are using a switch
statement to define rules determining which report is printed. For
demonstration purposes we print the standard purchase order report in
all cases except when the current company is CEU. In this case, we print a newly created PurchPurchaseOrder2. In real environments this code has to be modified, so that it reads the report name from some parameters table, for example VendFormletterParameters, which is a data source of Accounts payable | Setup | Forms | Form setup. The rest of the code runs the report from code.
The final bit is to modify
all menu items that are used to run the report. For a purchase order
there are three menu items— PurchPurchaseOrder, PurchPurchaseOrderCopy, and PurchPurchaseOrderOriginal—
that allow opening the purchase order document report. We need to make
sure that our class is called here. Same principle could be applied for
other purchase and sales business documents.