Tuesday, 4 June 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.

No comments:

Post a Comment

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...