Search This Blog

Monday, September 12, 2011

AX2012 Unit of Work

A record gets RecId value at the moment it is inserted into the database. What if we are doing bulk insert of a journal with many lines and the journal header contains some totals calculated based on its lines. It is impossible to insert lines before header since the value of the journal's surrogate key is unknown. But that would be so convenient because otherwise the header should be inserted first, then the lines and then the header should be updated with the calculated totals.

The answer is – use Unit of Work. 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

Let us do some sample-
Create the following Tables


















After Creating the tables create a job for doing the insertion for the above tables using UnitOfWork.




















 















Some more details about the unit of work feature:
All database operations happen only when unitofwork.saveChanges() method is invoked.
  1. UnitOfWork class has insertOnSaveChanges(), updateOnSaveChanges() and deleteOnSaveChanges() methods for CUD operations.
  2. Surrogate keys can be propagated automatically to related records if buffers are linked via navigation methods (AJournalLine.header() method in the example). Navigation methods can be created automatically from relations. I’ll write a separate post about them.
AX client has the great support of the unit of work as well.
This means that form datasources can be grouped into a unit of work.

No comments:

Post a Comment

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.