Header Ads Widget

🔍 How to Get FormRun, Controls, Datasources & Selected Records in D365FO via Event Handlers

In D365FO, event handlers provide a powerful way to interact with forms, controls, and data sources. Below are examples demonstrating how to utilize these event handlers effectively.





​

1. Accessing FormRun and Selected Record in a DataSource Event Handler

To interact with the FormRun and access the selected record within a datasource event handler:​

x++
[FormDataSourceEventHandler(formDataSourceStr(MyForm, MyDataSource), FormDataSourceEventType::Written)] public static void MyDataSource_OnWritten(FormDataSource sender, FormDataSourceEventArgs e) { FormRun formRun = sender.formRun() as FormRun; MyTable myTableRecord = sender.cursor(); // Example: Call a custom method on the form formRun.myCustomMethod(); }

2. Accessing a DataSource in a Form Event Handler

To retrieve a datasource within a form-level event handler:​d365ffo.com+1d365ffo.com+1

x++
[FormEventHandler(formStr(MyForm), FormEventType::Initialized)] public static void MyForm_OnInitialized(xFormRun sender, FormEventArgs e) { FormDataSource myDataSource = sender.dataSource(formDataSourceStr(MyForm, MyDataSource)); // Example: Access the current record MyTable myTableRecord = myDataSource.cursor(); }

3. Accessing a Form Control in a Form Event Handler

To manipulate a form control during form initialization:​

x++
[FormEventHandler(formStr(MyForm), FormEventType::Initialized)] public static void MyForm_OnInitialized(xFormRun sender, FormEventArgs e) { FormControl myControl = sender.design().controlName(formControlStr(MyForm, MyControl)); // Example: Set the control to invisible myControl.visible(false); }

4. Accessing FormRun from a Control Event Handler

To obtain the FormRun from a control's event handler:​

x++
[FormControlEventHandler(formControlStr(MyForm, MyButton), FormControlEventType::Clicked)] public static void MyButton_OnClicked(FormControl sender, FormControlEventArgs e) { FormRun formRun = sender.formRun() as FormRun; // Example: Call a custom method on the form formRun.myCustomMethod(); }

5. Accessing the Current Record in a Control Event Handler

To retrieve the current record from a datasource within a control's event handler:​

x++
[FormControlEventHandler(formControlStr(MyForm, MyButton), FormControlEventType::Clicked)] public static void MyButton_OnClicked(FormControl sender, FormControlEventArgs e) { MyTable myTableRecord = sender.formRun().dataSource(1).cursor(); // Example: Use the record's data info(strFmt("Current record ID: %1", myTableRecord.MyField)); }

📌 Tips for Working with Event Handlers

  • Use formDataSourceStr and formControlStr: These functions help avoid hardcoding strings and reduce errors.​

  • Leverage formRun(): This method provides access to the form's runtime object, allowing interaction with other controls and datasources.​

  • Access Current Records with cursor(): The cursor() method retrieves the current record from a datasource.​

  • Maintain Separation of Concerns: Keep business logic separate from UI logic to enhance maintainability.​filardi.cloud


By utilizing these event handler techniques, you can effectively interact with forms, controls, and data sources in D365FO, leading to more dynamic and responsive applications.

Post a Comment

0 Comments