0

I have copied an event (klicked) and are trying to do a validation of delivery mode, but the original code gets executed fore the validation. (Even though I have breakpoints activated!)

Does any one have any idea why?

class tt042WHSShipmentDetailsForm_Events
{

    [FormControlEventHandler(formControlStr(WHSShipmentDetails, btnOutboundShipConfirm), FormControlEventType::Clicked)]
    public static void btnOutboundShipConfirm_OnClicked(FormControl sender, FormControlEventArgs e)
    {

        Dialog dialog = new Dialog();
        WHSShipmentTable shipmentTable;
        FormDataSource dataSource = sender.formRun().dataSource(formDataSourceStr (WHSShipmentDetails, WHSShipmentTable));
        shipmentTable = dataSource.cursor() as WHSShipmentTable;
        dialog.addText("@tt_042:ScanDlvMode");
        sender.enabled(false);
        DialogField validationField= dialog.addFieldValue(extendedTypeStr (RetailDeliveryModeID), "" ,"@tt_042:DeliveryField");
        if (dialog.run())
        {
            if (dialog.closedOk())
            {
                RetailDeliveryModeID validate=validationField.value();
                if (SalesTable::find(shipmentTable.OrderNum).DlvMode!=validate)
                {
                    throw Global::error("@tt_042:ScanError");
                }
            }
        }
    }
}
Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
ErikG
  • 1
  • You probably need to use Chain of Command (CoC), see [Class extension - Method wrapping and Chain of Command - Controls](https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/extensibility/method-wrapping-coc#controls). – FH-Inway Feb 07 '20 at 19:15
  • Does the CoC apply though this isn't an extension? – ErikG Feb 08 '20 at 16:07
  • 1
    I was suggesting to use CoC instead of the event handler. – FH-Inway Feb 09 '20 at 13:21

1 Answers1

1

Event handlers and chain of command both do not override the original code. The only scenario in which the original code does not execute is if it is decorated with the attribute [Replaceable], which is exceptionally rare and almost nonexistent in the standard Microsoft models.

What they do is run code before or after the method you are targeting. In an event handler scenario, "post" handlers run after the method has executed (your situation), where "pre" handlers run before the method's code. Neither scenario will skip the out of the box code. In your specific scenarios, the OnClicked event executes after the out of the box clicked() method has finished executing. Chain of command is similar, and can be thought of as inheritance with a mandatory super() call. The next() call is required and your code either runs before or after it, but it does not skip it.

What this means is to skip execution of certain code you must do one of the following:

  1. Create a new button with altogether new logic,

  2. Request a delegate from Microsoft and they will create a delegate + if(_delegateResult) { micorosft.outOfTheBoxMethod();} statement so that if your delegate runs it can return false and will skip the out of the box execution,

  3. Sometimes you can get lucky and there are validation methods that you can put a post handler on and skip out of the box execution, but this requires a clever dig through the OOTB code to see if it is possible.

rjv
  • 1,058
  • 11
  • 29
  • Thank you for your response @rjv! you were right. i had to make a next call. From the error message i saw that the next call could only by making a extension. So instead of copying the event, i made a extension of the clicked method. This solved my problem:) – ErikG Feb 13 '20 at 13:56