AX / D365FO – Prevent from closing Form after pressing OK button with canClose()

In simple dialogs we want the form to validate the newly inserted fields when the OK button is pressed and not close if one of these contains an incorrect value.

In simple dialogs we cannot avoid closing the form unless we override the canClose() method

Follow this example to reach the goal.

    public boolean canClose() //Override the canClose() method of the form
    {
        boolean ret;
    
        ret = super();
    
        FormControl         frmCtrl = element.design().controlName('ParentLoadId'); //Get the FormControl by its name
        FormStringControl frmStrCtrl = frmCtrl as FormStringControl; //Cast it to correct type (in this case we have a FormStringControl
 
        if(!frmStrCtrl.validate()) //Call te field validate() method
        {
            ret = false; //If validation fails the canClose() is false 
        }

        return ret;
    }

Leave a comment