Pessimistic Locking in AX:

I was doing more work on the Quality Hold project I mentioned in my previous post, and I came across this error:

Cannot edit a record in <table description> (tablename). Cannot call NEXT, update(), or delete() on buffer where data is selected or inserted in another transaction scope.

This is what my code looked like:

Now the line that calls this.createPickListJournal() is calling a bunch of functions that are going to perform the return to stock, and that’s certainly going to change InventSum and do so in a transaction. However, that transaction hasn’t been created before the method is called, and is certainly closed by the time the function returns. So why is it locking my table? I tried moving my code above this.createPickListJournal();, and that didn’t make any difference.

I did a google search and several of the pages weren’t helpful, until I came across this page by Martin Drab:

https://community.dynamics.com/365/financeandoperations/b/goshoom/posts/ax-support-for-pessimistic-locking

Turns out, the InventSum Table has a special flag set to make it so my code doesn’t work:

Because of this flag, the InventSum table uses pessimistic locking instead of optimistic locking. In other words, since the table can be used in so many places Axapta requires you to perform additional steps to ensure that you have the most recent version before you save it.

Luckily there’s an easy solution to this. By rereading the table in my transaction I’m able to convince AX that I really do have the current version and that I should be allowed to proceed with my update:

Alternatively I could have put select statement inside my transaction statments, and that would have worked as well.

Leave a comment