Friday, April 23, 2021

Assign default values to form in D365FO

 

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.

No comments:

Post a Comment