Tuesday 14 July 2015

Catch insert, update or delete. Database log

Sometimes there is a need to find out, which class or other element in system is inserting, updating or deleting a record in a table which is interesting for us.

The easiest way is just to put a breakpoint on insert(), update() or delete() method of that table.
If there is no such method, it has to be added of course.
When some piece of code will call a method on table, we will see a full stack, and probably a reason why this table has been modified and where.

An example of insert() method on SalesTable and a breakpoint at the beginning.


The problem is, when a record is inserted/updated/deleted using doInsert(), doUpdate(), doDelete().
In that case, such breakpoint on table method will not work.

To find out what changes data in our table, we have to put a breakpoint in class Application
There are many methods, but for us, there are three interesting: logDelete(), logInsert() and logUpdate().

And in one of them we should put a breakpoint.

An example of breakpoint on logInsert()


But it's not enough just to put a breakpoint.

We have to go to 'System administration\Database\Database log setup'.
There will be a form with listed tables for which system is saving a log of changes and a type of those changes (Insert/Update/Delete).

Let's say that we want to find when a record is inserted to a table SalesTable using doInsert() method.

We have to press "New" button on Database log setup form
It will start a Wizard.
Just press Next.
It will take a while to build a table structure.
In my opinion it would be much more useful, just to see a list of tables with their AOT names and without any structure. Much faster, that's for sure, because this window with structure built for the first time appears after about 10 minutes! 

Please remember, that you will see a list of tables sorted by modules, and you will see their printed names. It's important because I had two AX, both in English language, but one was in UK English, and one in US English. And of course those tables had different printed names.

So, I need to catch a doInsert on SalesTable, so I have to choose a table "Sales orders"

Press Next

On next window please tick a checkbox "Track new transactions" for row with table name "Sales orders". There may be only one row, but on older working environments there may be more



Press Next, and on next (last) window of that Wizzard, please press Finish.

There was a breakpoint few lines above on that blog on logInsert() method of Application class.

So, after this database log setup has been made for SalesTable, a breakpoint will stop a trace, and it will be ease to find out what process has inserted a record to our table.


1 comment:

  1. This is very helpful! I can't believe I have been working for 5 years with AX and just learnt about this!
    A new day with ax everyday

    ReplyDelete