Get formRun, Form control, datasource and selected record from form datasource using Eventhandlers on Form in D365

How to Get FormRun, Form Controls, DataSource, and Selected Records Using Event Handlers in Dynamics 365 F&O

Author: Song Nghia – Technical Manager

In Dynamics 365 Finance and Operations, developers often need to interact with forms, controls, and datasource records when implementing custom business logic.

With the extension model in D365 F&O, we typically use event handlers instead of overlayering. Event handlers allow developers to access important objects such as:

  • FormRun
  • FormDataSource
  • FormControl
  • Selected record from datasource

This article demonstrates several practical examples for retrieving these objects in different event scenarios.



1. Get FormRun, DataSource, and Selected Record from FormDataSource Event

When handling datasource events such as Written, Validated, or Modified, we can retrieve the form instance and the currently selected record.

[FormDataSourceEventHandler(formDataSourceStr(MyForm, MyRandomTableDS), FormDataSourceEventType::Written)]
public static void MyRandomTableDS_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)
{
    FormRun formRun = sender.formRun() as FormRun;

    // Call custom method in form
    formRun.myCustomMethod();

    // Get selected record
    TableName tableBuffer = sender.cursor();

    // Get datasource variable
    FormDataSource dsVariable = sender.formRun().dataSource("TableName");
}

Explanation:

  • sender.formRun() returns the current FormRun instance.
  • sender.cursor() retrieves the selected record in the datasource.
  • dataSource() allows access to another datasource within the form.

2. Get Form DataSource from xFormRun

During form initialization, we can retrieve a datasource directly from xFormRun.

[FormEventHandler(formStr(SomeForm), FormEventType::Initialized)]
public static void SomeForm_OnInitialized(xFormRun sender, FormEventArgs e)
{
    FormDataSource myRandomTable_ds = sender.dataSource(
        formDataSourceStr(SomeForm, MyRandomTableDS));
}

This method is commonly used when modifying datasource behavior during form initialization.


3. Access Form Controls from xFormRun

Developers can dynamically manipulate form controls such as visibility or enable/disable status.

[FormEventHandler(formStr(SomeForm), FormEventType::Initialized)]
public static void SomeForm_OnInitialized(xFormRun sender, FormEventArgs e)
{
    sender.design().controlName(
        formControlStr(SomeForm, MyControl)).visible(false);
}

In this example, the control is set to invisible when the form is initialized.


4. Get FormRun from Form Control Event

When handling control events such as button clicks, we often need to retrieve the parent form.

[FormControlEventHandler(formControlStr(MyForm, MyButton), FormControlEventType::Clicked)]
public static void MyButton_OnClicked(FormControl sender, FormControlEventArgs e)
{
    FormRun formRun = sender.formRun() as FormRun;

    formRun.myCustomMethod();
}

5. Get Current Record from Form Control Event

In many cases, developers need to retrieve the currently selected record when a button is clicked.

[FormControlEventHandler(formControlStr(SomeForm, SomeButton), FormControlEventType::Clicked)]
public static void SomeButton_OnClicked(FormControl sender, FormControlEventArgs e)
{
    SomeTable callerRec = sender.formRun().dataSource(1).cursor();
}

Although datasource index can be used, it is recommended to reference datasources using formDataSourceStr() for better maintainability.


Conclusion

Using event handlers in Dynamics 365 Finance and Operations allows developers to extend form behavior without modifying standard objects.

By understanding how to access FormRun, FormDataSource, FormControl, and selected records, developers can implement flexible and maintainable customizations in D365 F&O.


Contact Us

Need help with Dynamics 365 Finance & Operations?

We provide professional services for:

  • Dynamics 365 F&O customization and development
  • ERP implementation support
  • System integration
  • Technical consulting and outsourcing

Song Nghia
Technical Manager

🌐 Website:
https://www.songnghia.com

If you need support for your Dynamics 365 projects, feel free to contact us.

Post a Comment

2 Comments

  1. https://denistrunin.com/d365fo-buildrelease/

    ReplyDelete
  2. Integrated access with CCTV and alarms in Singapore is perfect for high-security environments. integrated access control Singapore with CCTV and alarm system

    ReplyDelete