Dynamics Search Engine

Sunday, May 27, 2012

How to use Event Handler in Microsoft Dynamics AX 2012

How to use Event Handler in Microsoft Dynamics AX 2012

Applied on:
Microsoft Dynamics AX 2012 CU2.

Assumption:
This article is for those who has at least basic data flow, navigation and programming
knowledge of Dynamics AX.

About this article:
This article may answers your question like:-

• In Microsoft Dynamics AX 2012 how to handle event on different methods / occurrences.

• In Microsoft Dynamics AX 2012 how to implement or apply pre or post event handler.

• How to develop an event handler step by step in Microsoft Dynamics AX 2012.

• What is new in Microsoft Dynamics AX 2012 from programming point of view.


• X++ Event handling in Dynamics AX 2012.



Microsoft incorporated lot of beautiful new features in Dynamics AX 2012 and one of them is Event Handler. It’s a very nice feature of Dynamics AX 2012 which allows you trigger an event after or before an activity.

There are different programming paradigms like:
Observation: Where based on a particular behavior occurrence events can be used. For example if a new customer is created, based on this you need to update a table record and intimate to user.

Information dissemination: This is related to trigger an event at right time, right information to get expected result.

Decoupling: One producer's event can be acted upon by any number of consumers. Consumers can act upon any number of events from many different producers.


You should locate natural customization points and use events to publish the relevant information to subscribers.

You should invoke / defining events over using pre or post events on methods.

Pre-Event Handlers and Post-Event Handlers

An event handler can reside underneath a method node can run either before or after the method runs. You can use CalledWhen property on the event handler node. The CalledWhen property has two values:

• Pre – The event handler runs before the method starts.
• Post – The event handler runs after the method ends.

A new class the XppPrePostArgs Parameter is being used by event handlers.

A pre-method event handler that has only an XppPrePostArgs parameter can inspect and change the values of the parameters.

Similarly a post-method event handler that has only an XppPrePostArgs parameter can inspect and change the return value from the method.


When an XppPrePostArgs object is used, the values of the parameters and the return type can be changed by the event handler. The values can be changed even if the parameters and return type are value types, such as an integer or string. If a parameter or a return type is a reference to an object, the event handler can call methods on the object and could change the state of the object.


Event handlers can run only on the same tier as the publisher class of the delegate runs on. For instance, if the publisher class has its RunOn property set to Server, to declare a method with client keyword and call a subscribed event handler method is not allowed.

You can use X++ event as well as .Net made managed code event. You can define it at Event handler proper called EventHandlerType.


How to use event handlers in Microsoft Dynamics AX 2012 step by step:

1) Open your new developer work space and go to AOT then classes node.

2) Right click on Classes node and click on New Class as shown below.


3) By default system will give a name to it. Here in my case it’s Class1. Right click on newly created class and click on Rename shown below.


4) After clicking Rename, give a name called CustTableEventHandler to it. Here I am going to develop an event to be applied on CustTable so that is the reason why I decided this name (CustTableEventHandler). After renaming this class, it looks as shown below.


5) Right click on class CustTableEventHandler then New then Pre- or post-event handler as shown below.


6) Once you click on this, system gives you a method as shown below.


7) Customize the method as shown below.

8) Here args is providing current record to custTable instance and info is displaying the current customer account. The code snippet is below.

public static void custCreateInfo(XppPrePostArgs _args)
{
     CustTable custTable;
     custTable = _args.getThis();

     info(strFmt("Customer account %1 has been created", custTable.AccountNum));
}

This method I support to call from insert method of CustTable with type post event. It means that once insertion is done to CustTable, system will display recently inserted customer account number. It depends on your business requirement what logic you want to apply here. So here you can develop your required business logic or invoke pre built logic.

9) Go to Tables node in AOT then find out CustTable.

10) Go to insert method of CustTable and right click on it then click on New Event Handler Subscription as shown below.


11) After clicking you will get a new Event handler shown below.


12) Rename the event handler to custCreateInfo and set the property as shown below.


13) Now save your work.

14) Go to Customer form and create a new customer. Here I created a new customer account called “Test-000001”.


15) Once this customer is created system will give your infolog as shown below.


Hope this will help you to understand the event handler in Microsoft Dynamics AX 2012.

Friday, May 11, 2012

Microsoft Dynamics AX 2012 Cubes or Role center BI report error

Microsoft Dynamics AX 2012 Cubes or Role center BI report error.

In Microsoft Dynamics AX 2012 you might face problem with BI reports for role center or reports where Cubes are involve.

There can be number of reasons for this and you will get number of errors. Like permission not granted, access denied, related data missing, cubes not processed etc.

Two main reasons are (1) data missing in respective tables and (2) insufficient license.

In most of the places data is interrelated among the tables. If you are missing related data in tables, it will give some errors.

Secondly, by default Microsoft provides cubes for all the tables and assumption is you have 100% license. But when there is a limited license and you apply it to your application, based on limited license system drops few tables for which you do not have license. In such a scenario your queries for cubes are not updated and they are still using 100% tables but you don’t have 100% tables now because of limited license.

Here the option for you is to update the queries but it’s a huge task. For Microsoft Dynamics AX 2009 you have to do it manually but to help you Microsoft published a tool to generate an excel sheet which tells you what are the areas you need to work.
In case of Dynamics AX 2012, Microsoft provided few options in the form of a wizard from where you can do it by clicking a button as shown below:
Path: Tools Business Intelligence (BI) Tools  SQL Server Analysis Services project wizard.

In the above wizard if you select Update option and follow the wizard it resolve your problem.