Wednesday 30 January 2013

What is RecID


What is RecId?
The RecId is a unique field in every table, used as an identifier. Every row in the system can be guaranteed (in theory) to have a unique RecId. RecIds can be negative, and their value can change due import/export operations. Due to this, it is not a good idea to use RecIds as foreign key references to other tables.

What is the difference between a RecId and an Id?


The kernel generates the RecId while the Id is generated by the application.

Microsoft Dynamics AX 2012 Eventing



Microsoft Dynamics AX 2012 Eventing - a look with X++

This will give an overview of eventing with Dynamics AX 2012, further more I have given some examples with Pre, Post and Custom Raised Events to help getting more understanding.

For my example, I'm actually doing straight X++, with no relation to any real world application yet. The idea is to understand what the mechanics are, and then from there apply those in design and in development as needed.

You will notice, in the above project, I have a few classes, called Books & BooksPicker. I think that this would make this a little bit more fun, than the standard hello world!

So let’s take some time, to examine the Books class within my project. 

Here we see the class declaration, in which I have two variables I'm making use of. A Set, called bookCart and an int called book. This is setting up, for later use, within the business logic of my Books class.

Next we move forward to the New method of the Apples class.

With it, we see that I'm setting the variables that we saw in the class declaration, to initialize these variables, for the Set and int types.

Moving forward from there, we go to the next method, called addBooktoCart(). This is where some of the fun starts.

Here we see I'm taking the init book, and adding one to it with the ++; call. Next we move to actually adding an "book" to our bookCart set, with the int of apple as part of the key.

Moving from there, we see a final, what looks to be a method call, that is the following.:

"this.bookCollected(book);"

In looking at this, it would seem as if this was a normal method, and it behaves as such, however this is not a method, but a delegate I created. We can get a peak, inside the delegate I created, for the Books class, with the following screen shot.

Notice that it is blank actually and not doing anything. Its truly a delegate, and looking at this, within the project, we will see that it appears differently, that the rest of the methods within the class.

You can create delegates within the AOT space of a given element, as we see on the Books class in the above image. You do this, as if you were creating a new method.

What you should also notice, is the fact that below it, we have an event handler subscriber that we have created in the same fashion.

Diving deeper into the event handler subscription element, we can see that it has a property page, that allows us to take and set the class, type of, being X++ or Managed, and then the method to call. 

Based on this setup for the event handler subscription element, we can then see it's calling a static method, called pickBook from the BooksPicker class. 

Now it's very important to point out, that all event handler subscription elements must be static methods. These are the only types allowed, for create such an event handler subscriber, or event listener.

So, what this is doing then, is when the Books.addBookToCart() is called, within it, via X++ code, we call to a delegate, of Books.bookCollected(). This delegate, has an event handler subscription setup, for the BooksPicker.pickBook() method.

Let’s now take a look inside that static method, which represents our event subscriber, called pickBook().

As you can see from the method, we are taking a parameter of int bookId, which happens to be the exact method signature of the delegate that is firing this event handler. This method simply takes and calls to the info log, claiming that whatever the id value, is that book has now been picked!

Seeing this in action, as part of this class, I have a job, with the following contents.

In here we see a simple job, that takes and creates a new instance of the Books class, and contains a “for loop” of 10 iterations, of adding books to cart. Since our delegate will be called, after a book is added to the Set, then we see a message similar to the one below, which was fired each time, from the event handler pickBook().

Now moving beyond the mechanics of this process, this can then been applied in several instances of AX. Typically, in other languages, we see such event handlers, as when buttons are clicked, or just before or after a method is fired.

That is actually an option you can do, for any method, you can setup a pre or post event handler subscription for a given method, which will fire on the pre or post processing of another given method.

We can also bring this eventing to a functional level, for say when a Sales Order or greater than $10,000.00 is invoiced, an event is fired that automatically places an order for a pizza party, for the dept. :-)

A little elementary school humor there, but if you really start to think about how this can be used, then the power of such abilities with Microsoft Dynamics AX 2012, comes to life.

Hope this examples will help in understanding the Eventing in AX.

Tuesday 22 January 2013

Client/Server method modifier and ‘Run On’ property of object

Client /Server Basically these are the method modifiers use to minimized the load/traffic between client and server. These modifiers can only be used for static methods of class and table static methods. These can’t be use for instance methods because these method will be run on where the class instance method created. Also we can’t’ use these modifiers for table methods insert, doInsert, update, doUpdate, delete, and doDelete on the Server (where the data source is).

Since we have Client tier (AX Client) and Server tier (AOS), so if we want to execute our method on client then we can set the method modifier to ‘client’ and if we want to execute the method on server than we can set the modifier to ‘server’.Also if don’t set and modifier to the method then method will be run based on the ‘RunOn’ property of the class.If the RunOn property of the class is set to ‘Called From’ then method can either be executed on client or server.












// Sets the RunOn property to Client.
client static boolean myMethod()
{
    // ToDo Insert code here.
}
Above method will be run on AX client

// Sets the RunOn property to Server.
server static boolean myMethod()
{
    // ToDo Insert code here.
}
Above method will be run on AOS
 
But we can also do this(see below) which apparently make the property of the method to ‘Called from’
 
// Sets the RunOn property to Called from.
client server static boolean myMethod()
{
    // ToDo Insert code here.
}

Example1(Client modifier):
We have a table called ‘JournalizingDefinition’ which has a static method ‘lookupRefCtrlJournalizingDefByModule’ which has a ‘Client’ modifier declare. Now this method will always we be used for a form lookup field on a ‘Transaction Posting Definition’ form













Transaction Posting Definition form





Now on a lookup field of the form this method is being used several times for different module(see below).So basically it means that this method will never be used on Server side that’s why it is set to Client(AX Client)















Example 2 (Server modifier):
We have a class ‘LedgerJournalPost’ which has a method called ‘Post’ which is apparently used when we try to ‘Post’ a journal.


 
















So it mean that this method will always be called on Server AOS for posting purposes.



Wednesday 16 January 2013

Super() method in AX 2012




 
Basically we can use super() in terms of inheritance.It is nothing just a parent call from a child method.Lets take couple of examples

Example 1:
Use of super() in Form’s method.
I have a form called ‘JournalizingDefinition’ ,contains many methods  under a Methods node.







When click on method node we will see
















So lets view the method called ‘init’(when form initializes ,this first method that is called is init method)


So there is super method, so the reason why super() is here,is that on creation of an instance of the any form, this super() method called the parent kernel method ‘loadUserSetting’ method of the SysSetupFormRun class,which creates and loads the form which is necessary otherwise form wont be initalize.Try to comment this super() method and open the form…

Example 2:
Use of super() in Form’s datasource method

JournnalizingDefinition form has multiple datasources(tables) attach to it.















So lets focus on ‘JournalizingDefinition; datasource and click the methods notes you will see the above methods.
Notice there is a ‘validateWrite’ ,method in which a super() is called



Now navigate to the tables node and search for ‘JournalizingDefinition’ and click method nodes











You will see the same method ‘validateWrite as well.But this is the parent method.

Now go to Form’s dataSource JournalizingDefinition open the validateWrite method and select the super() method and insert breakpoint.



Now open the form and insert the record and save button.I will open the debug mode and super() method is higlighted


Now press F11.Now it will take you the parent method validateWrite of the table journalizingDefinition,
Below is the parent method under ‘JournalizingDefinition’ method
public boolean validateWrite()
{
    boolean ret;

    ret = super();

    return ret;
}