Friday, 27 October 2017

Chain of Command–next() gotcha

UPDATE: 31/10/2017

Microsoft reached out and I have to correct my example code. It seems the compiler does not catch it when it is 2 nested conditions. See below code.



Be careful when using Chain of Command not to place the next() call in a condition.

Here is an example.

Create a class extension for a form. Call the method init() but place it in an “if” condition. Something like this.

 [ExtensionOf(formStr(PurchReqCreate))]  
 final class PurchReqCreate_EAM_Extension  
 {  
   public void init()  
   {  
     info("Outer - Before next");  
     if (this.args() &&  
       this.args().record() &&  
       this.args().record().TableId == -100) //intentionally made it -100 to not execute this code  
     {    
       //doing something here  
       info("Inner - Before next");  
       if (this.args().record().TableId == -100)   
       {  
         info("Inside second if condition");  
       }  
       next init();  
       info("Inner - After next");  
     }  
     info("Outer - After next");  
   }  
 }  


The compiler will not error or give you a warning. However, at run time you will get a misleading error.

In my case I got this. “Error executing code: Wrong argument type for function.”

image

Having a look at Event viewer (Folder: AX-XppRuntime), it gave me a little more info. At least I know the class that was calling it. I searched for any customization.

image

I found an extension class PurchReqCreate_Extension. I was able to eye ball and figure out the problem. Taking next() outside the “if” condition.

Rewrote the same:

public void init()
{
  if (this.args() && this.args().record())
  {
    //
do something before
  }

  next init();

  if (this.args() && this.args().record())
  {
    //
do something after
  }

}

3 comments:

Ivan Kashperuk said...

Thanks for reporting this.
We'll look into it

Marcos Calderon said...

Could you provide us with the Platform build number you are using. I can not reproduce this issue, the compiler always provides the proper error "call to 'next' should be done only once and unconditionally". And as a rule, you should not call next in a condition.

Munib Ahmed said...

Installed product version : Microsoft Dynamics 365 for Finance and Operations, Enterprise edition (July 2017)
Installed platform version : Update11 (7.0.4679.35176)

This is an issue if you have an inner condition. Updated above and emailed you

Thanks