Header Ads Widget

✅ How to Fetch Multi Selected Records on Button Click in D365FO

 

🔍 Introduction

In Dynamics 365 Finance and Operations (D365FO), you often need to perform operations on multiple selected rows in a grid, such as bulk processing or exporting. The best way to do this in a form button’s clicked() event is by using the MultiSelectionHelper class.

This blog provides a rewritten X++ code example to retrieve selected records using a Form Button Clicked event handler, ensuring uniqueness and clean naming conventions.


🛠️ Event Handler to Get Multiple Selected Rows in Grid

x++
[FormControlEventHandler(formControlStr(SalesTableListPage, FormButtonControl1), FormControlEventType::Clicked)] public static void FormButtonControl1_OnClicked(FormControl _clickedButton, FormControlEventArgs _eventArgs) { SalesTable selectedRecord; FormDataSource salesDS = _clickedButton.formRun().dataSource(formDataSourceStr(SalesTableListPage, SalesTable)); MultiSelectionHelper selectionHelper = MultiSelectionHelper::construct(); selectionHelper.parmDatasource(salesDS); selectedRecord = selectionHelper.getFirst(); while (selectedRecord.RecId != 0) { info(strFmt("Selected Sales ID: %1", selectedRecord.SalesId)); selectedRecord = selectionHelper.getNext(); } }

🧠 Explanation

  • FormControlEventHandler: Binds your logic to the button click.

  • MultiSelectionHelper: A built-in utility class to iterate selected records.

  • getFirst() / getNext(): Used to loop through all selected rows.

  • info(): Displays each SalesId as feedback — you can replace this with any custom logic.


💡 Use Case Scenarios

  • Approving multiple sales orders

  • Exporting selected rows to Excel

  • Applying bulk updates to selected records


📌 Best Practices

  • Always validate selection before processing.

  • Keep button logic modular; call a service class if logic gets lengthy.

  • Use meaningful variable names and comment your code for maintainability.


🧾 Summary

By using the MultiSelectionHelper class inside a Clicked() event handler, you can efficiently fetch and process multiple selected records in any list/grid-based form. This approach is clean, upgrade-friendly, and avoids custom form control extensions.

Post a Comment

0 Comments