Pages

Tuesday, 4 June 2013

How to Use a Report Data Provider Class in a Report (AX 2012)


imageimage
image

A report data provider (RDP) class is an X++ class that is used to access and process data for a report. An RDP class is an appropriate data source type when the following conditions are met:
  • You cannot query directly for the data you want to render on a report.
  • The data to be processed and displayed is from Microsoft Dynamics AX.
For more information, see Guidance for Choosing the Data Source Type.
A report parameter provides a way to choose report data, connect related reports together, and vary the report presentation. It is used when generating the report data set. The parameters that an RDP class will reference are defined in a data contract class. The following two classes are required to be able to setReport Data Provider as your data source type on a report with parameters.
  • Data Contract Class – defines the parameters in the report.
  • Report Data Provider Class – processes business logic based on a query and parameters defined in the data contract class, and then returns the tables as a dataset for the report.

A data contract is an X++ class that has getters, setters and the DataContractAttribute attribute. You create the RDP and data contract classes in the MorphX code editor. For information about how to create an X++ class, see Declaration of Classes.
The following example illustrates the code to declare a data contract class.

[DataContractAttribute]
public class SrsRDPContractSample
{
    AccountNum accountNum;
    CustAccountStatment accountStmt;
    boolean inclTax;
}

A data contract class has methods with the DataMemberAttribute attribute. The name that follows the attribute is the parameter name that displays in Visual Studio when you bind a report data set to the RDP class. Add a method named parmAccountNum in code editor.
The following example illustrates the code for the parmAccountNum method.
[DataMemberAttribute("AccountNum")]
public AccountNum parmAccountNum(AccountNum _accountNum = accountNum)
{
    accountNum = _accountNum;
    return accountNum;
}

Add a method named parmAccountStmt in code editor.
The following example illustrates the code for the parmAccountStmt method.
[DataMemberAttribute("CustAccountStatement")]
public CustAccountStatement parmAccountStmt(CustAccountStatement _accountStmt = accountStmt)
{
    accountStmt = _accountStmt;
    return accountStmt;
}
Add a method named parmInclTax in code editor.
The following example illustrates the code for the parmInclTax method.
[DataMemberAttribute("InclTax")]
public boolean parmInclTax(boolean _inclTax = inclTax)
{
    inclTax = _inclTax;
    return inclTax;
}

To define a report data provider class

An RDP class extends the SRSReportDataProviderBase class. You set the SRSReportParameterAttribute attribute to the data contract you created for the RDP class. The data contract defines the parameters that the report uses. You set the SRSReportQueryAttribute attribute to the query specified in theparmQuery method in the RDP class.
The following example illustrates an RDP class declaration that uses the SrsRDPContractSample data contract class and the Cust query.
[
    SRSReportQueryAttribute('Cust'),
    SRSReportParameterAttribute(classstr(SrsRDPContractSample))
]
public class SrsRdpSampleClass extends SRSReportDataProviderBase
{
    TmpCustTableSample tmpCust;
}

Your code will get parameters from the end user, process business logic to generate data in a table and then return the table to render in the report. You will define a method that returns a table of the data that you process in the RDP class.
A table returned by a method can be a temporary table (InMemory or TempDB) or a regular table. When the data returned is used for reporting only, it is a best practice to use a temporary table.
  • Use an InMemory temporary table if the data set is small, like 1000 records.
  • Use a TempDB temporary table for large data sets to improve performance.
You can have one or more methods return tables.
NoteNote
If the data returned depends on the company context the report is run from, then the tables returned by the RDP class must have the table propertySaveDataPerCompany set to Yes.
The following example illustrates an RDP class method named getTmpCustTable.
[SRSReportDataSetAttribute('TmpCust')]
public TmpCustTableSample getTmpCustTable()
{
    select * from tmpCust;
    return tmpCust;
}

You will provide the report business logic in the processReport method. Override the processReport method to provide business logic for your report.
The following example illustrates how the processReport method computes data and populates the data tables that will be returned to Reporting Services.
public void processReport()
{
    AccountNum              accntNum;
    CustAccountStatement    custAcctStmt;
    boolean                 boolInclTax;
    Query                   query;
    QueryRun                qr;
    QueryBuildDataSource    queryBuildDataSource;
    QueryBuildRange         queryBuildRange;
    CustTable               queryCustTable;

    SrsRdpContractSample    dataContract;

    // Get the query from the runtime using a dynamic query.
    query = this.parmQuery();
        
    // Get the parameters passed from runtime.
    dataContract = this.parmDataContract();
    accntNum = dataContract.parmAccountNum();
    custAcctStmt = dataContract.parmAccountStmt();
    boolInclTax = dataContract.parmInclTax();
        
    // Add parameters to the query.
    queryBuildDataSource = query.dataSourceTable(tablenum(CustTable));
        
        
    if(accntNum)
    {
        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(accntNum);
    }        
        
    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.
    qr = new QueryRun(query);
    ttsbegin;
    while(qr.next())
    {
        tmpCust.clear();
        queryCustTable = qr.get(tablenum(CustTable));
        tmpCust.AccountNum = queryCustTable.AccountNum;
        tmpCust.Name = queryCustTable.name();
        tmpCust.Address = queryCustTable.address();
        tmpCust.CustGroup = queryCustTable.CustGroup;
        tmpCust.Phone = queryCustTable.phone();
        tmpCust.InvoiceAccount = queryCustTable.InvoiceAccount;
        tmpCust.AccountStatement = queryCustTable.AccountStatement;
        tmpCust.InclTax = queryCustTable.InclTax;
        tmpCust.insert();
    }
    ttscommit;
}
The next step is to create a report using the Visual Studio tools for Microsoft Dynamics AX. For more information, see Creating Reports Overview. For the complete steps to bind a report data provider class to a report, see Walkthrough: Creating a Report Bound to a Report Data Provider Class (X++ Business Logic).

No comments:

Post a Comment