Вы находитесь на странице: 1из 6

Lab 2.

21: Create a Report with X++ Business Logic

In this lab you will use a report data provider (RDP) class with business logic to
process data and then display the outcome of the business logic on a report. An
RDP class is an X++ class that is used to access and process data for a Reporting
Services report.

Scenario

Develop a report to display customer data with parameters for account number,
account statement and whether to include tax.

Challenge Yourself

Use the information that is provided in the scenario to develop the report.

Need a Little Help?


1. Create a temporary table
2. Define a report data provider class
3. Define the report parameters
4. Define a method to return data to Reporting Services
5. Add business logic for the report
6. Create a reporting project
7. Bind a report to a report data provider class

Step by Step: Create a Temporary Table


1. Open Microsoft Dynamics AX.
2. Press Ctrl+Shift+W to enter the development environment.
3. In the AOT, expand the Data Dictionary node, right-click the Tables
node, and then click New Table.
4. Right-click the table and click Properties.
5. In the Properties window, set the Name property to
TmpCustTableSample and set the Table Type property to TempDB.
This will define the table as a SQL Server temporary table.
6. Expand the node next to the TmpCustTableSample table so that you
can see the Fields node.
7. Press Ctrl+D to open another AOT window and move the window so
that you can see both AOT windows.

1
Source: Microsoft Dynamics AX 2012 Reporting, Chapter 2
8. In the second AOT, expand the Data Dictionary node, expand the
Extended Data Types node, and drag the following types to the
Fields node in the first AOT window:
o AccountNum
o CustName
o LogisticsAddressing
o CustGroupID
o Phone
o CustInvoiceAccount
o Action Days
o InclTax
9. In the second AOT window, expand the Base Enums node and drag
the CustAccountStatement enumeration to the Fields node of the
first AOT window.

Step by Step: Define a Data Contract Class


1. In the AOT, right-click the Classes node, and then select New Class.
2. Right-click the new class, click Rename, and then enter
"SrsRDPContractSample".
3. Expand SrsRDPContractSample, right-click classDeclaration, and
then click View Code.
4. In code editor, enter the following code in the class declaration to
define the class.

[DataContractAttribute]
public class SrsRDPContractSample
{
AccountNum accountNum;
CustAccountStatement accountStmt;
Boolean inclTax;
}
5. Save the class and close the editor.

Step by Step: Define a Data Contract Method


1. Right-click SrsRDPContractSample, point to New, and then click
Method.
2. Edit the method so that it contains the following code.

[DataMemberAttribute("AccountNum")]
public AccountNum parmAccountNum(AccountNum _accountNum
= accountNum)
{
accountNum = _accountNum;
return accountNum;
}
3. Save the method and close the editor.
4. Right-click SrsRDPContractSample, point to New, and then click
Method.
5. Edit the method so that it contains the following code.

[DataMemberAttribute("CustAccountStatement")]
public CustAccountStatement
parmAccountStmt(CustAccountStatement _accountStmt =
accountStmt)
{
accountStmt = _accountStmt;
return accountStmt;
}
6. Save the method and close the editor.
7. Right-click SrsRDPContractSample, point to New, and then click
Method.
8. Edit the method so that it contains the following code.

[DataMemberAttribute("InclTax")]
public boolean parmInclTax(boolean _inclTax = inclTax)
{
inclTax = _inclTax;
return inclTax;
}
9. Save the method and close the editor.

Step by Step: Define a Report Provider Class


1. In the AOT, right-click the Classes node, and then click New Class.
2. Right-click the new class, click Rename, and then enter
"SrsRDPSampleClass".
3. Expand SrsRDPSampleClass, right-click classDeclaration, and then
click View Code.
4. In the code editor, edit the class so that it contains the following
code.
[
SRSReportQueryAttribute (querystr(Cust)),
SRSReportParameterAttribute(classstr(SrsRDPContractSample))
]
public class SrsRdpSampleClass extends SRSReportDataProviderBase
{
TmpCustTableSample tmpCust;
}
5. Save the class and close the editor.
Step by Step: Define a Method to Return the Data to Reporting Services
1. Right-click SrsRdpSampleClass, point to New, and then click
Method.
2. Edit the method so that it contains the following code.

[SRSReportDataSetAttribute("TmpCust")]
public TmpCustTableSample getTmpCustTable()
{
select * from tmpCust;
return tmpCust;
}
3. Save the method and close the editor.

Step by Step: Add Business Logic for the Report


1. Right-click SrsRdpSampleClass, point to Override method, and then
click processReport.
2. Edit the method so that it contains the following code.

public void processReport()


{ AccountNum accountNumber;
CustAccountStatement custAcctStmt;
boolean boolInclTax;
Query query;
QueryRun queryRun;
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
CustTable queryCustTable;
SrsRdpContractSample dataContract; // Get the query from the runtime using a
dynamic query.
// This base class method reads the query specified in the SSRS
ReportQueryAttribute attribute.
query = this.parmQuery(); // Get the parameters passed from runtime.
// The base class methods read the SRSReportParameterAttribute attribute.
dataContract = this.parmDataContract();
accountNumber = dataContract.parmAccountNum();
custAcctStmt = dataContract.parmAccountStmt();
boolInclTax = dataContract.parmInclTax(); // Add parameters to the query.
queryBuildDataSource =
query.dataSourceTable(tablenum(CustTable)); if(accountNumber)
{
queryBuildRange =
queryBuildDataSource.findRange(fieldnum(CustTable, AccountNum)); if
(!queryBuildRange)
{
queryBuildRange =
queryBuildDataSource.addRange(fieldnum(CustTable, AccountNum));
} // If an account number has not been set, then use the parameter value to set
it.
if(!queryBuildRange.value())
queryBuildRange.value(accountNumber); } if(custAcctStmt)
{ queryBuildRange = queryBuildDataSource.findRange(fieldnum(CustTable,
AccountStatement));
if (!queryBuildRange)
{
queryBuildRange = queryBuildDataSource.addRange(fieldnum(CustTable,
AccountStatement));
} // If an account statement has not been set, then use the parameter value to
set it.
if(!queryBuildRange.value())
queryBuildRange.value(int2str(custAcctStmt)); } if(boolInclTax)
{ queryBuildRange =
queryBuildDataSource.findRange(fieldnum(CustTable, InclTax)); if
(!queryBuildRange)
{
queryBuildRange = queryBuildDataSource.addRange(fieldnum(CustTable,
InclTax));
} // If flag to include tax has not been set, then use the parameter value to set it.
if(!queryBuildRange.value())
queryBuildRange.value(int2str(boolInclTax)); } // Run the query with modified
ranges.
queryRun = new QueryRun(query); ttsbegin;
while(queryRun.next())
{ tmpCust.clear();
queryCustTable = queryRun.get(tablenum(CustTable));
tmpCust.AccountNum = queryCustTable.AccountNum;
tmpCust.CustName = queryCustTable.name();
tmpCust.LogisticsAddressing = queryCustTable.address();
tmpCust.CustGroupId = queryCustTable.CustGroup;
tmpCust.Phone = queryCustTable.phone();
tmpCust.CustInvoiceAccount = queryCustTable.InvoiceAccount;
tmpCust.CustAccountStatement = queryCustTable.AccountStatement;
tmpCust.InclTax = queryCustTable.InclTax;
tmpCust.insert(); }
ttscommit; }
1. Save the method and close the editor.

Step by Step: Create a Reporting Project


1. Open Microsoft Visual Studio.
2. On the File menu, point to New, and then click Project. The New
Project dialog box is displayed.
3. In the Installed Templates pane, click Microsoft Dynamics AX. In the
Templates pane, click Report Model.
4. In the Name box, type SampleRDPReport, and in the Location box,
type a location.
5. Click OK.

Step by Step: Bind a Report to a Report Data Provider Class


1. In Solution Explorer, right-click the SampleRDPReport node, point to
Add and then click Report.
2. In Model Editor, right-click the Report1 node, and then click
Rename.
3. Type "CustomerReport" as the name.
4. Right-click the Datasets node, and then click Add Dataset.
5. In the Properties window, specify the following values.
o Data Source: Dynamics AX
o Data Source Type: Report Data Provider
o Default Layout: Table
o Name: Customer
o Dynamic Filters: True

This setting is for dynamic parameters on the report. Setting the property to
True allows you to filter the report by setting a range on any fields from the data
source table.

6. For the Query property, click the ellipsis button (…). A dialog box
displays where you can select an RDP class that is defined in the
AOT and identify the fields that you want to use. Select the
SrsRDPSampleClass class and click Next. In the Select Fields dialog
box, keep all the check boxes selected, and then click OK.
In Model Editor, select the Customer node and drag it onto the Designs node. An auto design named
AutoDesign1 is created for the report. Based on what you select, the Query property value is updated. In
this case, the query is SELECT * FROM SrsRDPSampleClass.TmpCust.