In this blog, I will try to explain how to write a code for validating write event handler for a form DataSource level using X++ in D365FnO.
To begin, create a new class and name it “VendGroup_FormDS_EventHandler“. Then, copy the code provided below into the newly created class.
class VendGroup_FormDS_EventHandler
{
[FormDataSourceEventHandler(formDataSourceStr(VendGroup, VendGroup), FormDataSourceEventType::ValidatingWrite)]
public static void VendGroup_OnValidatingWrite(FormDataSource sender, FormDataSourceEventArgs e)
{
// You can also get FormDataSource and Cursor from commented code.
/*
FormRun formRun = sender.formRun();
FormDataSource vendGroup_ds1 = formRun.dataSource(formDataSourceStr(VendGroup, VendGroup)) as FormDataSource;
VendGroup vendGroup1 = vendGroup_ds1.cursor();
*/
FormDataSource vendGroup_ds = sender as FormDataSource;
var eventArgs = e as FormDataSourceCancelEventArgs;
if (eventArgs != null && vendGroup_ds != null)
{
VendGroup vendGroup = vendGroup_ds.cursor() as VendGroup;
if (vendGroup.GroupRegion == "")
{
boolean ret = checkFailed("FormDataSourceLevel - Vendor region must be filled.");
eventArgs.cancel(ret);
}
}
}
}
Please refer to the following screenshot of the error: