VALIDATE event handler Form D365 FormDataSourceEventType::ValidatingWrite

VALIDATE event handler Form D365 FormDataSourceEventType::ValidatingWrite


How to write a FormDataSourceEventType validate write data event handler code for a Form
 /// <summary>
     /// When saving a new rental, prevent setting the start mileage on the FMRental form to a value that is equal to 1
     /// </summary>
     [FormDataSourceEventHandler(formDataSourceStr(FMRental, FMRental), FormDataSourceEventType::ValidatingWrite)]
     public static void FMRental_OnValidatingWrite(FormDataSource sender, FormDataSourceEventArgs e)
     {
         var datasource = sender as FormDataSource;
         var args = e as FormDataSourceCancelEventArgs;
         if (args != null && datasource != null)
         {
             FMRental record = datasource.cursor() as FMRental;
             if (record.recId == 0)
             {
                 if(record.startmileage == 1)
                 {
                     boolean doCancel = !checkFailed("Start Mileage = 1 is not allowed");
                     args.cancel(doCancel);
                 }
             }
         }
     }
Below is the code at the table level for table FMVehicle
 Class FMVehicleEventHandlers //write any name of the class
 {
     /// <summary>
     ///
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
 [DataEventHandler(tableStr(FMVehicle), DataEventType::ValidatedWrite)]
     public static void FMVehicle_onValidatedWrite(Common sender, DataEventArgs e)
     {
         ValidateEventArgs validateArgs = e as ValidateEventArgs;
         FMVehicle vehicle = sender as FMVehicle;
         boolean result = validateArgs.parmValidateResult();
         if (vehicle.NumberOfCylinders > 8)
         {
             result = checkFailed("Invalid number of cylinders.");
             validateArgs.parmValidateResult(result);
         }
     }
 }

Comments

Popular posts from this blog

Event handlers and post handlers in D365

Data entites method calling sequence in D365FO

How to Extend Sales Order Update Functionality to Custom Fields in D365 Finance and Operations