Tuesday, June 4, 2019

How to extend or overwrite a class method in D365 Finance and Operations X++?

In this example I will be showing how to extend or overwrite a class method.
In D365 7.1 and later versions Microsoft is not allowing to overwrite the standard objects. To overcome this, we need to use extensions.


Below example I'm overwriting/extending main method of SalesFormLetter class to add my custom logic to pop a dialog box while posting a picking slip.

Here, I'm executing my custom logic first.Then, executing standard logic.



[ExtensionOf(classStr(SalesFormLetter))]
final class TestClass_SalesFormLetter_Extension
{
    /// <summary>
    ///Rahul - on 06/04/2019
    /// Project @ TestClassForExtensions
    /// To display a dialogbox popup
    /// </summary>
    /// <param name = "args"></param>
    public static void main(Args  args)
    {
        RefRecId             record         = args.record().RecId;
        DocumentStatus     parmEnum       = args.parmEnum();
        Object             callerForm     = args.caller();
        MenuItemNameAction callerMenuItem = args.menuItemName();
        SalesTable         salesTable     = SalesTable::findRecId(record);
        DialogButton            diagBut;

        if(callerMenuItem == 'SalesFormLetter_PickingList_Action' || callerMenuItem == 'SalesFormLetter_PickingList')
        {
            if(SalesTable.SalesOriginId == 'CUSTOM' && SalesTable.PaymMode == 'Card' )
            {
                diagBut = Box::yesNo('Do you want to continue without charging this order', DialogButton::No, 'Title');
                if(diagBut == DialogButton::No)
                {
                    return;
                }
            }
        }

        next main(args);
    }

}


In the same way you can extend other methods as well.

1 comment:

  1. Very helpful explanation on extending methods in D365 F&O, especially the SalesFormLetter example. For testing and demos, using Random mailing addresses can also make scenarios more realistic without exposing real customer data.

    ReplyDelete

What is the primary purpose of using a Solution in Microsoft Power Platform & ALM?

As organizations embrace low-code development with Microsoft Power Platform , it becomes essential to manage and govern apps, flows, and dat...