69) Query based ssrs report with the Contract, controller and Ui builder class.

  •   I had a scenario where I need to create a new Asset work order report using query based but have the filter and the company image.


  • To complete this requirement we have create a contract class ( for dialog validation), Controller class To filter the query and the UI builder class for the dialog creation.
First create  query and View to show the data and after that :- 

Step 1 :- Create a UI Builder class :- 

/// <summary>
///    The <c>AvaAssetWorkorderHistoryReportUIBuilder</c> class is the UIBuilder class for the <c>AvaAssetWorkorderHistoryReport</c> report.
/// </summary>
[
    SrsReportNameAttribute('AvaAssetWorkorderHistoryReport.ReportCopy1'),
    SysOperationContractProcessingAttribute(classstr(AvaAssetWorkorderHistoryReportUIBuilder), SysOperationDataContractProcessingMode::CreateUIBuilderForRootContractOnly)
]
class AvaAssetWorkorderHistoryReportUIBuilder extends SrsReportDataContractUIBuilder
{
    #define.parameterFromDate('FromDate')
    #define.parameterToDate('ToDate')
    #define.parameterAssetID('AssetID')

    DialogField dialogFromDate;
    DialogField dialogToDate;
    DialogField dialogAssetID;

    AvaAssetWorkorderHistoryReportContract contract;
    /// <summary>
    ///     Builds the dialog for the <c>AvaAssetWorkorderHistoryReport</c> SSRS report.
    /// </summary>
    public void build()
    {
        Dialog dialogLocal;
       

        dialogLocal = this.dialog();
        contract = this.getRdlContractInfo().dataContractObject() as AvaAssetWorkorderHistoryReportContract;

        dialogLocal.addGroup();
        dialogFromDate = dialogLocal.addFieldValue(extendedTypeStr(FromDate),DatetimeUtil::date(contract.getValue(#parameterFromDate)), "@SYS5209","");
        dialogToDate = dialogLocal.addFieldValue(extendedTypeStr(ToDate),DatetimeUtil::date(contract.getValue(#parameterToDate)), "@SYS14656");
        dialogAssetID =  dialogLocal.addFieldValue(extendedTypeStr(EntAssetObjectID), (contract.getValue(#parameterAssetID)), "AssetID","");

        if (controller.parmArgs().record())
        {
            EntAssetObjectTable                 assetTable;
     
            assetTable = controller.parmArgs().record() as EntAssetObjectTable;
            
            if (assetTable)
            {
                dialogAssetID.value(assetTable.ObjectID);// dialogLocal.addFieldValue(extendedTypeStr(EntAssetObjectID), assetTable.ObjectID);
                dialogAssetID.allowEdit(false);

                contract.setValue(#parameterAssetID,assetTable.ObjectID);
            }
        }
       
    }

    /// <summary>
    ///    Transfers data from the dialog into the data contract object.
    /// </summary>
    public void getFromDialog()
    {
        contract.setValue(#parameterFromDate, DateTimeUtil::newDateTime(dialogFromDate.value(), 0));
        contract.setValue(#parameterToDate, DateTimeUtil::newDateTime(dialogToDate.value(),86399));
        if(dialogAssetID.value())
        {
            contract.setValue(#parameterAssetID, dialogAssetID.value());
        }
        else
        {
            contract.setValue(#parameterAssetID,'');
        }
    }

}
========================================================================

Step 2 : - Create a contract class for the validation of from date and To date 

/// <summary>
///    The <c>AvaAssetWorkorderHistoryReportContract</c> class is the contract class for the <c>AvaAssetWorkorderHistoryReport</c> report.
/// </summary>
[
    SrsReportNameAttribute('AvaAssetWorkorderHistoryReport.ReportCopy1'),
    SysOperationContractProcessingAttribute(classStr(AvaAssetWorkorderHistoryReportUIBuilder),
    SysOperationDataContractProcessingMode::CreateUIBuilderForRootContractOnly)
]
    
    class AvaAssetWorkorderHistoryReportContract extends SrsReportRdlDataContract
{
    #define.parameterFromDate('FromDate')
    #define.parameterToDate('ToDate')

    /// <summary>
    ///    Validates the parameters.
    /// </summary>
    /// <returns>
    ///    true if successful; otherwise, false.
    /// </returns>
    public boolean validate()
    {
        boolean ret = super();

        if (this.getValue(#parameterFromDate) && this.getValue(#parameterToDate))
        {
            // Check that the FromDate is greater than ToDate
            if (this.getValue(#parameterFromDate) > this.getValue(#parameterToDate))
            {
                ret = checkFailed("@SYS16982");
            }
        }

        if (!this.getValue(#parameterFromDate))
        {
            ret = checkFailed("@SYS97591");
        }

        if (!this.getValue(#parameterToDate))
        {
            ret = checkFailed("@SYS97592");
        }

        return ret;
    }

}

======================================================================

Step 3 :- Create a controller class to define the report details and filter the query :-

/// <summary>
///    The <c>AvaAssetWorkorderHistoryReportController</c> class starts the Asset workorder history report.
/// </summary>
class AvaAssetWorkorderHistoryReportController extends SrsReportRunController
{
    #define.ReportName ('AvaAssetWorkorderHistoryReport.ReportCopy1')
    #define.parameterFromDate('FromDate')
    #define.parameterToDate('ToDate')
    #define.parameterAssetID('AssetID')


    /// <summary>
    ///    Override this method to change the report contract before running the report.
    /// </summary>
    protected void preRunModifyContract()
    {
        AvaAssetWorkorderHistoryReportContract    contract = this.parmReportContract().parmRdlContract() as AvaAssetWorkorderHistoryReportContract;
        Query               query = this.getFirstQuery();//this.parmReportContract().parmQueryContracts().lookup(this.getFirstQueryContractKey());
        FromDate            fromDate = contract.getParameter(#parameterFromDate).getValueTyped();//contract.getValue(#parameterFromDate);
        ToDate              toDate =  contract.getParameter(#parameterToDate).getValueTyped(); 
        EntAssetObjectID    assetID = contract.getParameter(#parameterAssetID).getValueTyped();

        
       // SysQuery::findOrCreateRange(query.dataSourceTable(tableNum(EntAssetWorkOrderTable)), fieldNum(EntAssetWorkOrderTable, ExpectedStart)).value(queryRange(fromDate, toDate));

      
    
        if (assetID)
         {
             query.clearQueryFilters();
             // Modify the query contract based on fromDate & toDate.
             SrsReportHelper::addFromAndToDateRangeToQuery(query,
                                                           fromDate,
                                                           toDate,
                                                           tableNum(EntAssetWorkOrderTable),
                                                           fieldNum(EntAssetWorkOrderTable, ExpectedStart));

             // Modify the query contract based onAssetID.
             SrsReportHelper::addParameterValueRangeToQuery(query,
                                                            tableNum(EntAssetObjectTable),
                                                            fieldNum(EntAssetObjectTable, ObjectID),
                                                            assetID);
         }
    
        else
        {
            query.clearQueryFilters();
            // Modify the query contract based on fromDate & toDate.
            SrsReportHelper::addFromAndToDateRangeToQuery(query,
                                                        fromDate,
                                                        toDate,
                                                        tableNum(EntAssetWorkOrderTable),
                                                        fieldNum(EntAssetWorkOrderTable, ExpectedStart));

   
        }
        super();
    }

    public static AvaAssetWorkorderHistoryReportController construct(Args _args)
    {
        AvaAssetWorkorderHistoryReportController controller = new AvaAssetWorkorderHistoryReportController();
        controller.parmReportName(#ReportName);
        controller.parmArgs(_args);
        return controller;
    }

    public static void main(Args args)
    {
        AvaAssetWorkorderHistoryReportController::construct(args).startOperation();
    }

}

======================================================================

Step 4 :- Create a Action menu item for the controller class please look at the below screen shot :-


=======================================================================

Next step is create a parameter in Report design level and design your report :-

Please review the below screen shot :-


=========================================================================















Comments

Popular posts from this blog

Customization on Sales invoice Report in D365 F&O

75) COC - Create a coc of the table modified method

46) D365 FO: SHAREPOINT FILE UPLOAD USING X++