Monday, February 4, 2019

Chain of Command - avoiding the next call

This is possibly something that needs to be used very sparingly if at all, but apparently there is a way (up until the latest version - v8.1) to avoid calling the next() call when using chain of command.

The key is to use double nested conditions:

//this will not work "call to 'next' should be done only once and unconditionally"
    protected void chooseLinesPackingSlip(boolean _append)
    {
       If (true)
{
           this.somethingElse(_append);
           return;
       }

       next chooseLinesPackingSlip(_append);

    }

//this will work
    protected void chooseLinesPackingSlip(boolean _append)
    {
       If (true)
{
    if (true)
    {
               this.somethingElse(_append);
               return;
           }
       }

       next chooseLinesPackingSlip(_append);

    }

2 comments:

  1. Very interesting Hendy.
    As the documentation suggests: "Wrapper methods in an extension class must always call next, so that the next method in the chain and, finally, the original implementation are always called. This restriction helps guarantee that every method in the chain contributes to the result."
    This might be suitable for prototyping, but you could encounter problems when this is combined with other extensions. So hopefully this won't break other extensions by you and others.
    https://docs.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/extensibility/method-wrapping-coc#wrapper-methods-must-always-call-next
    - Chris Garty (Microsoft - F&O PM - Platform Extensibility)

    ReplyDelete
    Replies
    1. Hi Chris, thanks for your comment.
      I understand that requesting Microsoft to add the [Replaceable] attribute into the method is probably the best way to go forward instead of doing this

      Delete