/ Microsoft / Assign default values to form in D365FO

Assign default values to form in D365FO

Learn, share and spread knowledge

To assign default values to form in Microsoft Dynamics 365 for Finance and Operations is very common requirement. Event handlers in D365FO is very useful to implement the logic and fulfill the requirements of the clients.

Importantly, there are many event handlers for different purposes. To assign default values to form in D365FO, I am using OnInitValue form data source event handler. This event handler is triggered when form is initializing the fields of the data sources those belonged to that form.

In addition to the above, you can also add the criteria or conditions before assigning the default values. You can assign values by fetching it from somewhere in the system. You can do many things regarding initialization of the fields using this event handler.

Scenario:

I am assigning a value to a field in HcmEmploymentLeave form using OnInitValue form data source event handler.

Lets start!

  • Run Visual Studio
  • Search your desired form in Application Explorer
  • Right click the form -> click Open designer
  • Expand the Data Sources node
  • Expand your desired data source node -> Events -> Select OnInitValue event -> Right click and copy event handler method
  • Create a new class
  • Paste the event handler method
  • Add code in it like below:
[FormDataSourceEventHandler(formDataSourceStr(HcmEmploymentLeave, HcmEmploymentLeave), FormDataSourceEventType::InitValue)]
public static void HcmEmploymentLeave_OnInitValue(FormDataSource sender, FormDataSourceEventArgs e)
{
     FormRun formRun = sender.formRun();
     Object hcmEmploymentLeave_ds = formRun.dataSource(formDataSourceStr(HcmEmploymentLeave, HcmEmploymentLeave)) as FormDataSource;
     HcmEmploymentLeave hcmEmploymentLeave = hcmEmploymentLeave_ds.cursor();
     
     //Assigning value to the field
     hcmEmploymentLeave.FIELDNAME= VALUE;
}

Above code is just an example to show you how it works. You can apply these steps to any form.

So, in this way you can use OnInitValue form data source event handler in D365FO to assign default values to form.

Moreover, if this helps you, please Like, Comment and Share to help other people.

If you found any ambiguity or a better solution, please feel free to ask.

Blog: Click here

YouTube: Click here

GitHub: Click here

He who plants a tree, plants a hope. – Lucy Larcom

1 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

1 Comments