Saturday, October 5, 2013

Be careful in while using menuFunction class to run a menu Item using X++ code

Hi Friends,
Many times while developing/customizing processes in Microsoft Dynamics AX, we are required to call forms through X++ code. Sometimes people use MenuFunction class and sometimes ClassFactory.

In case you use menu function class then be careful as recently I faced a situation where system was crashing at menuFunction.run() call. In this situation we found classFactory() to be working fine. This situation arised when AOS and Client were on different machines.

I will explain the scenario and reasons now with the below example:
  • Create a new class and a static function which always runs on a client. Inside this function we are creating an instance of MenuFunction class to run the display menu item "CustGroup"




  • Call this function from a job as shown below you will notice that form will open:

  • Change the function declaration to run it on server as shown below
  • Now let's use classFactory to run the form and again change the function declaration to run the function on client

  • Run the job and the form opens up.
  • Now change the function definition to run the function on server.

  • Run the job and the form opens up.
Reason I found is that classFactory is having run on property as "Called from" so it can be initialized either from client or from server. However in standard AX most menu items have run on property set as "Client". See below the display menu item custGroup properties:



 So if you try to instantiate menuFunction class from a code running on server to run a menu item which runs on client then your system will stop responding and ultimately crash, however classFactory runs fine in both the situations.

To summarize the scenario:
  • AOS and client are on different machines which is mostly a real time situation.
  • The called menu item has run on property set as client (Most standard AX display menu items are set this way)
  • The menu function was instantiated from code running on server side. 
  • In case menu function is instantiated from a code running on client, then it works fine.
I would suggest to prefer using classFactory rather than menuFunction class to call a form as it works always :). However please consider security implications of using classFactory.

P.S -> For testing the above scenario make sure AOS and Client are on different machines. If everything is installed in one box then this scenario can't be replicated.

Thanks for reading the blog.

Cheers!!!