Search This Blog

Thursday, 6 December 2012

Unit of Work Framework In Microsoft Dynamics AX 2012

Unit of Work Framework is use to commit number of records in a single Transaction.The main reason for introducing unit of work is that it is impossible to insert the lines table record before its header table  because there is a relation between the header table and the lines table.The Recid of the header table is comes as a foreign key in the child table.

Lets took an example as you can see in the microsoft dynamics ax there are two tables i.e custtransopen and custtransopenline.These two tables are related with each other CustTrabsOpen is the parent table and custtransopenline is the child table.

As you can see in the below image about  these two tables and there relations.


There is a common field on both the tables i.e amountcur.

In the parent table i.e in the custtransopen table this amountcur contains the total of the custtransopenlines amountcur.
Now we cant insert the records in the custtransopenline table because it needs the recid of the parent table i.e custtransopen and similarly we cant insert thet record first in the parent table i.e custtransopen because it needs the total amountcur from the custtransopenline.

To resolve this issue Unit of Work Franework is introduced.It allows to perform create, update and delete operations without worrying about the order of those and without a need to specify surrogate key values. It will all be done automatically in the kernel!

Now to show the Implementation of the Unit Of Work I have created two tables i.e invoiceheader and invoiceline and than made the relation between them.The invoiceheader contains the field i.e totalquantity which holds the total quantity of the invoiceline field i,e quantity.

Below image will show you these two tables,their fields and there relations.
























Now the below  job will insert the invoiceheader with calculated totals and 2 lines. Note, that there is no place where recid is mentioned in the code. Business data only.







































One thing that I need to mention there is that.In the above code I have write the code i.e "line.invoiceheader(header);"

This is the method use to link the invoiceline table and the parent table.To call this method we have to first set the property CreateNavigationPaneProperty to "Yes".As shown in the image below:



Now When I run this job an exception will occured as you can see in the below image.

























I have run a job due to this issue the exception is occured to resolve this issue I have to make a static method inside the class and also include server tags in that method.as you can see in the below image.







Now when I call this method the data will be inserted successfully.you can see the below image of tables which contains the data that I have inserted.
 



 

 
 




































Thanks

Muhammad Zahid.

2 comments: