Conflict error when list of fields is used in select during update transaction

If you use this code, you will get an error on the line 29. The error message is following:

Cannot edit a record in Table1 (Table1).
An update conflict occurred due to another user process deleting the record or changing one or more fields in the record.

  1: static void updateTable1(Args _args)
 2: {
 3:    Table1 tbl;
 4:    ;
 5:
 6:    ttsbegin;
 7:    select firstonly forupdate tbl;
 8:
 9:    if (tbl.RecId)
10:    {
11:        tbl.InfoText = "Reset";
12:        tbl.update();
13:    }
14:    else
15:    {
16:        tbl.InfoText = "Reset";
17:        tbl.IntField = 1;
18:        tbl.insert();
19:    }
20:    ttscommit;
21:
22:    tbl.clear();
23:
24:    select firstonly RecId, InfoText from tbl;
25:
26:    ttsbegin;
27:    tbl.selectForUpdate(true);
28:    tbl.InfoText = "Table1 - field select";
29:    tbl.update();
30:    ttscommit;
31: }

The reason for this is that each time the table is updated the field RecVersion is updated with a random number that is saved by kernel. That why when you try to update the record, kernel cannot update record partly, what results in update conflict.

There are two ways to avoid this issue:

1. Add RecVersion field into the selection

 24:    select firstonly RecId, InfoText, RecVersion from tbl;

2. Use selectforupdate keyword

 24:    select firstonly forupdate RecId, InfoText from tbl;

Martin F

Comments

  • Anonymous
    March 02, 2010
    Hello, Martin.. I have same problem Thank you for your solution. But I dont understant , Which table should I make the change or Method? Best Regards

  • Anonymous
    March 22, 2010
    I have a problem in that the error above is displayed ONLY when I call my Dynamics X++ class method from the .NET business connector, using .NET transaction commands.  The code works if I remove the .NET transaction commands, but this is not desired as I need to repeat the method several times before committing my changes.   Interestingly however, my code works if I call it from a dynamics Job, and can loop through the class method several times without it complaining - all the while containing the loop within transaction statements. Is there a difference between the transaction commands inside Dynamics AX and those in .NET business connector?

  • Anonymous
    May 04, 2011
    Just to add some more information on the post. I believe some times when you update a update even after coming out from ttscommit the recIdVersion is still not updated and if you run an another update on the same records you may end up with the same error. To resolve this ..if you use table.reread() method just before you are going to update the record second time it will reread the data from the SQL with an updated recversion and allows you to update without the error. Cheers!