Use a Report Data Provider Class in a Report [AX 2012]

Lasă un comentariu

3 Ianuarie 2014 de liviustoica55


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 set Report 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.

 

To define a data contract class


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.

X++

 

[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.

X++

 

[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.

X++

 

[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.

X++

 

[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 the parmQuery method in the RDP class.

The following example illustrates an RDP class declaration that uses the SrsRDPContractSample data contract class and the Cust query.

X++

 

[

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.

Note

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 property SaveDataPerCompany set to Yes.

The following example illustrates an RDP class method named getTmpCustTable.

X++

 

[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.

X++

 

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).

 

How to: Add Validation to a Data Contract Class [AX 2012]

This topic has not yet been rated – Rate this topic

Updated: December 16, 2011

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

If you want to provide additional validation, you can implement the SysOperationValidatable interface that provides a validate method for a data contract. This topic describes how to validate a data contract class for the following scenarios:

Type of Data Contract

Description

Report Definition Language (RDL) Data Contract

A report is bound to a query, data method, or OLAP data source and validation logic must be added for the parameters.

Report data provider (RDP) Data Contract

A report is bound to an RDP class and validation logic must be added for the parameters.

Adding Validation to RDL Data Contracts


For RDL data contracts, the SrsReportRdlDataContract class implements default validation that will do basic parameter validation. It will also validate the company and culture parameters. When you override this class, first call the super() method to allow the base class to validate, and then implement your validation logic. It is required to specify the SrsReportNameAttribute attribute using the following syntaxSrsReportNameAttribute(ssrsreportstr(<report>, <design>)). The SysOperationContractProcessingAttribute attribute is optional. Supply the SysOperationContractProcessingAttribute attribute to use a custom UI builder with the contract class. Otherwise, the framework UI builder will be used to render the UI for dealing with the parameters when the report is run.

To add validation to an RDL data contract

  1. In the Microsoft Dynamics AX Development Workspace, open the AOT. Right-click the Classes node and then click New Class.
  2. Double-click the new class to open the classDeclaration in Code Editor.
  3. An RDL data contract must extend the SrsReportRdlDataContract class and implement the SysOperationValidatable interface. The naming convention is [ReportName]RDLContract. The following code example illustrates the classDeclaration of theHcmi9DocumentExpireListRdlContract data contract class that is used for the HCM i9 document expire list report. The FromDate and ToDate are examples using date effectivity.

    X++

     

    /// <summary>

    /// The <c>Hcmi9DocumentExpireListRdlContract</c> class is the <c>RdlContract</c> class for the

    /// <c>Hcmi9DocumentExpireList</c> report.

    /// </summary>

    [

    SrsReportNameAttribute(‘Hcmi9DocumentExpireList.Report’),

    SysOperationContractProcessingAttribute(classStr(Hcmi9DocumentExpireListUIBuilder))

    ]

    class Hcmi9DocumentExpireListRdlContract extends SrsReportRdlDataContract implements SysOperationValidatable

    {

    #define.parameterFromDate(‘FromDate’)

    #define.parameterToDate(‘ToDate’)


     

  4. In the AOT, right-click the new class you created, point to New, and then click Method.
  5. In the Code Editor, provide validation logic for the parameters. The following example illustrates the validation logic for theHcmi9DocumentExpireListRdlContract data contract class parameters.

    X++

     

    /// <summary>

    /// Validates the parameters.

    /// </summary>

    /// <returns>

    /// true if successful; otherwise, false.

    /// </returns>

    public boolean validate()

    {

    boolean isValid = super();

    // Check whether FromDate and ToDate parameters are getting values or not

    if(this.getValue(#parameterFromDate) && this.getValue(#parameterToDate) )

    {

    // Check whether FromDate is greater than ToDate or not

    if (this.getValue(#parameterFromDate) > this.getValue(#parameterToDate))

    {

    error(„@SYS91020”);

    isValid = false;

    }

    }

    return isValid;

    }

Adding Validation to RDP Data Contracts


For RDP data contracts, implement the validate method and return True or False to indicate whether the parameter is valid. It is required to attach the DataContractAttribute attribute. The SysOperationContractProcessingAttribute attribute is optional. Attach theSysOperationContractProcessingAttribute attribute to use a custom UI builder with the contract class. Otherwise, the framework UI builder will be used to render the UI for dealing with the parameters when the report is run. To report a validation error, use the error method and write to the infolog. The error messages in the infolog will be marshaled from the service to the client and then rendered. Do not throw an error.

To add validation to an RDP data contract

  1. In the Microsoft Dynamics AX Development Workspace, open the AOT. Right-click the Classes node and then click New Class.
  2. Double-click the new class to open the classDeclaration in Code Editor.
  3. An RDP data contract must implement the SysOperationValidatable interface. The naming convention is [ReportName]Contract. The following code example illustrates the classDeclaration of the AssetDueReplacementContract data contract class that is used for the asset due replacement report.

    X++

     

    /// <summary>

    /// The <c>AssetDueReplacementContract</c> class is used as the data contract for the <c>AssetDueReplacement</c> SSRS report.

    /// </summary>

    [

    DataContractAttribute,

    SysOperationContractProcessingAttribute(classStr(AssetDueReplacementUIBuilder))

    ]

    public class AssetDueReplacementContract implements SysOperationValidatable

    {

    CurrentOperationsTax postingLayer;

    AssetReplacementDate dateFrom;

    AssetReplacementDate dateTo;

    Name dimension;

    NoYes includeSubTotal;

    }

     

  4. In the AOT, right-click the new class you created, point to New, and then click Method.
  5. In the Code Editor, provide validation logic for the parameters. The following example illustrates the validation logic for theAssetDueReplacementContract data contract class parameters.

    X++

     

    /// Determines whether the parameters are valid.

    /// </summary>

    /// <returns>

    /// true when the parameters are valid; otherwise, false.

    /// </returns>

    public boolean validate()

    {

    boolean isValid = true;

    if (!dateFrom)

    {

    isValid = checkFailed(strFmt(„@SYS84753”, „@SYS180311”));

    }

    if (!dateTo)

    {

    isValid = checkFailed(strFmt(„@SYS84753”, „@SYS180217”));

    }

    if (dateFrom > dateTo)

    {

    isValid = checkFailed(strFmt(„@SYS300457”, date2StrUsr(dateFrom, DateFlags::FormatAll), date2StrUsr(dateTo, DateFlags::FormatAll)));

    }

    return isValid;

    }

 

Lasă un răspuns

Completează mai jos detaliile despre tine sau dă clic pe un icon pentru autentificare:

Logo WordPress.com

Comentezi folosind contul tău WordPress.com. Dezautentificare / Schimbă )

Poză Twitter

Comentezi folosind contul tău Twitter. Dezautentificare / Schimbă )

Fotografie Facebook

Comentezi folosind contul tău Facebook. Dezautentificare / Schimbă )

Fotografie Google+

Comentezi folosind contul tău Google+. Dezautentificare / Schimbă )

Conectare la %s

%d blogeri au apreciat asta: