Passing Multiple Selected Records from a Form to an SSRS Report in D365FO

Recently, I had a requirement in Dynamics 365 Finance & Operations (D365FO) to pass multiple selected records from a form to an SSRS report. To achieve this, I used the MultiSelectionHelper class, which allows you to retrieve all selected records from a form's grid.

 Step-by-Step Implementation

To implement this functionality, I created:

  • A contract class to hold the list of selected RecIds.

  • A controller class to fetch the selected records using MultiSelectionHelper.

  • A data provider (DP) class to process and insert the selected records into a temporary table for the report.

Contract Class

The contract class contains a list of RecIds:

List recid;


[

    DataMemberAttribute('recId'),

    SysOperationHelpTextAttribute(literalStr("Rec Id")),

    SysOperationLabelAttribute(literalStr("Rec Id")),

    SysOperationDisplayOrderAttribute('0'),

    AifCollectionType('return', Types::Int64)

]

public List parmRecId(List _recid = recid)

{

    recid = _recid;

    return recid;

}

Controller Class

In the controller class, I overrode the prePromptModifyContract() method to capture the selected records before the report is generated.

prePromptModifyContract() Method

This method is used to modify the contract by supplying parameters (in this case, the list of RecIds) based on the form’s selected records.

public void prePromptModifyContract()

{

    EmployeeIncrements        employeeIncrements;

    FormDataSource            callerDS;

    Common                    callerRecord;

    MultiSelectionHelper      helper = MultiSelectionHelper::construct();

    List                      selectedRecIds = new List(Types::Int64);

    IncrementLetterBulkContract contract;

    // Get the contract object

    contract = this.parmReportContract().parmRdpContract() as IncrementLetterBulkContract;

    // Get the form datasource and selected records

    callerRecord = this.parmArgs().record() as EmployeeIncrements;

    callerDS = FormDataUtil::getFormDataSource(callerRecord);

    helper.parmDatasource(callerDS);

    employeeIncrements = helper.getFirst();

    while (employeeIncrements.RecId != 0)

    {

        selectedRecIds.addEnd(employeeIncrements.RecId);

        employeeIncrements = helper.getNext();

    }

    // Set selected RecIds in the contract

    contract.parmRecId(selectedRecIds);

}


Data Provider (DP) Class

In the DP class, I used a list enumerator to loop through the selected records and insert them into a temporary table.

public void processReport()

{  

    delete_from tempTable;

    EmployeeIncrements incrementTable;

    IncrementLetterBulkContract contract = this.parmDataContract();

    List selectedRecs = contract.parmRecId();

    ListEnumerator enumerator = selectedRecs.getEnumerator();

    while (enumerator.moveNext())

    {

        select firstonly * from incrementTable

            where incrementTable.RecId == enumerator.current();

        this.insertData(

            incrementTable.CaseNo,

            incrementTable.EmployeeId,

            incrementTable.IncrementTypes

        );

    }

}

Conclusion

By using the MultiSelectionHelper class and passing selected RecIds via a custom contract, we can efficiently generate SSRS reports based on multiple selected records from a form. This approach is highly useful for bulk operations like printing letters, generating statements, or batch reports in D365FO.

Comments

Popular posts from this blog

JSON-Based HTTP POST Request from X++ to Retrieve an Access Token

Calling an External API from D365FO Using X++ to Retrieve a Token

Restricting Date Selection in Dynamics 365 FO using X++