D365F&O - X++ - How to Enable/Disable button on form based on a condition in D365F&O using Event handlers.

1- Create a new class SalesOrderFormEventHandlers.
2- Go to SalesTable form.
3- Expand SalesTable datasource.
4- Expand Events, right click on OnActivated and click on Copy event handler method.



5- Paste the code in your SalesOrderFormEventHandlers class.
6- Save and build your project.

class SalesOrderFormEventHandlers
{
    /// <summary>
    /// Enable or disable Refund button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
   
   
    [FormEventHandler(formStr(SalesTable), FormEventType::Activated)]
    public static void SalesTable_OnActivated(xFormRun sender, FormEventArgs e)
    {
        // Button control name in your form design
        #define.BUTTONNAME('JWRefundButton')
        FormDataSource form_ds = sender.dataSource(formdatasourcestr
            (SalesTable, SalesTable));
        
        // Get selected sales order record
        SalesTable salesOrder = form_ds.cursor();
       
        // Disable the button if the sales status of selected record is Canceled
        FormFunctionButtonControl RefundButtonControl = sender.control(
             sender.controlId(#BUTTONNAME));
        RefundButtonControl.enabled(salesOrder.SalesStatus == SalesStatus::Canceled);
    }

}

Comments

Popular posts from this blog

D365F&O - X++ - Best Practice Warning BP Rule: [BPErrorUnknownLabel]: Path:

D365F&O - X++ - Use of JumpRef() method in form extension