Unified Operations & Dynamics AX Forum

Expand all | Collapse all

How to extend a class method where change is needed in the middle

  • 1.  How to extend a class method where change is needed in the middle

    SILVER CONTRIBUTOR
    Posted 01-23-2018 04:09 PM
    ​We have custom in class ReqCalc method insertItemInventTrans in AX 2012. I am busy moving our custom to D365.
    This method has 123 lines.
    It builds a query and then it reads the query results in a while statement.

     while (queryRun.next())
                {
                    inventTrans       = queryRun.get(tableNum(InventTrans));
                    inventDim         = queryRun.get(tableNum(InventDim));
                    inventTransOrigin = queryRun.get(tableNum(InventTransOrigin));

                    if (_setup.isInventLocationNotCovPlanned())
                    {
                        inventLocation = queryRun.get(tableNum(InventLocation));
                        if (inventLocation.Manual)
                        {
                            continue;
                        }
                    }

                    if (!this.checkProbability(inventTransOrigin, probabilityId))
                    {
                        continue;
                    }

                    //<CUSTOM> -verify if there is a hold code against the transaction that must be skipped
                    if(this.TMC_ExcludeOrderHoldCode(inventTransOrigin.ReferenceCategory, inventTransOrigin.InventTransId))
                    {
                        continue;
                    }
                    //</CUSTOM>
                   .....
                   .....

    Then after our custom it continues with the rest of the method. As you can see in the above code we added custom to call a custom method to skip to the next iteration if it returns true.
    To extend this method (insertItemInventTrans), I need to use chain of command. However chain of command seems to only work when the custom needs to be added at the end of the existing method (post event handler simulation) or at the beginning of the existing method (pre event handler simulation), not in the middle like our code.

    Is there another way to extend a method where custom is needed in the middle of the method or will we have to use overlay until Microsoft split this method into smaller pieces so that we can extend it?
    If there is no other way then I will have to piggy-bag off of the method this.CheckProbability() which Microsoft is currently using to skip to the next iteration In the while-loop.
    I should be able to use chain of command on this.CheckProbability() because I can add onto it at the end.

    If anyone has a better solution, your input will be much appreciated.

    ------------------------------
    Retha Lambrechts
    The Master's Craft Flooring Company
    West Plains MO
    ------------------------------


  • 2.  RE: How to extend a class method where change is needed in the middle

    Posted 8 days ago
    Hi Retha,

    This requirement does not have a general solution. We cannot add the custom code in the middle of standard methods.
    So we need to do extensions (Pre/Post/COC) which is the opposite of adding custom code in the middle.

    I suggest if you use the COC on the this.CheckProbability() as u mentioned to skip those iterations.

    ------------------------------
    Mohammad El-Saheb
    dar al handasah shair and partners
    beirut
    ------------------------------