Tuesday, March 15, 2011

Error executing code: The method has been called with an invalid number of parameters

One of the great features of Dynamics AX is the rich integration with other applications or services. The Dynamics AX client is for example exchanging data with Microsoft Office applications like Microsoft Office Excel or Microsoft Office Outlook. Some of these interactions are performed using COM. When the Dynamics AX Client is using COM for the data exchange in certain situation it can happen you are getting confronted with one of the following error message:
  • Error executing code: The method has been called with an invalid number of parameters
  • The number of arguments provided is different from the number of arguments accepted by the method
Certain situations means, that you are not always getting this error message but only sometimes and the error is not reproducible. The chance of getting this error message is higher when you continue to work in other applications while the Dynamics AX client is performing the COM operation.
One reason for running into this issue can be if the Windows Operating System is replacing the Dynamics AX application window by a ghost window.
When Dynamics AX starts a lengthy COM operation, it is not responding to messages sent by the Windows Operating System in time. So Windows supposes Dynamics AX has stopped responding. When this happens the Dynamics AX application window is replaced by a ghost window until Dynamics AX resumes. Window ghosting is a nice feature that allows the user to minimize, move or close the main window even if the application is not responding. You can easily identify a ghost window as it shows (Not responding) in the window title.
Unfortunately the replacement of the Dynamics AX application window by the ghost window can interfere the COM operation and result in the above error message.
Therefore it can be helpful to turn window ghosting for the Dynamics AX Client off:
  1. Locate the Dynamics AX shortcut in the Windows Start Menu
  2. Click with the right mouse button on the shortcut and select Properties
  3. Go to the Compatibility tab
  4. Enable the checkbox Run this program in Compatibility mode for and select Windows XP (Service Pack 2)
Please note:
  • We have seen this issue most on Windows Vista and Windows Server 2008 but this issue is not limited to those two
  • Window ghosting is not the only cause of this issue, so you might continue to see the error message even if window ghosting was turned off
But sometime there exists several possibilities resulting in this situation and disabling Window Ghosting has proven as workaround in some of those. By now we have found some other workarounds as well. But so far there is no once for all solution.
Here are the workarounds we know so far...

Workaround 1: Disable Window Ghosting as described in this post.

Workaround 2: Debug the X++ code down to the line that is causing the error. Once identified, insert one line before this statement a call to the yield method of the xInfo class.
  Example:  infolog.yield();
  namespace = outlook.getNamespace(#mapidef);


Workaround 3: Debug the X++ code down to the line that is causing the error. Once identified, place the statement inside a try / catch block. In the catch block simply retry the operation.
  Example:  int retryCount = 3;
  //...

  try
  {
     namespace = outlook.getNamespace(#mapidef);
  }
  catch(Exception::Error)
  {
     retryCount--;

     if(retryCount > 0)
        retry;
     else
        throw(Exception::Error);
  }




1 comment:

Campi said...

Other solution, try to write a box question just before the line error:

if (box::yesNo("ok?",DialogButton::No) == DialogButton::Yes)
sysOutlookNameSpace = sysOutlook.getNamespace(#MAPI);