3

I understand that the table's data method creates a copy of a record including the system fields. Normally it should generate a new RecID, which is reasonable because otherwise you cannot insert the record in the same table.

Now I'm having a case where the data method is creating a copy which has the same RecId, leading to an error during the insert operation. I'm not sure why this is happening and would appreciate any input on this topic.

What I'm doing:

  • Duplicating an existing product configuration model Screenshot
  • When I run the debugger, at some point the execution thread reaches a table method PCClass.duplicateTranslation() enter image description here
  • In line 20 and 22 we can see that a duplicate record is created, which is then tried to insert, which fails because they have both the same RecId (see next screenshot) enter image description here

This is a standard AX method, so why isn't it working?

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
elToro
  • 1,003
  • 9
  • 31
  • 1
    Probably a bug, they should not use the `data` method here. Try using `buf2buf` instead. As far as I know, a new `RecId` is only created if `RecId` is 0. – FH-Inway Oct 29 '15 at 09:44
  • 1
    The use `data` is all okay, see `Docu::copy`. Some other index is involved. – Jan B. Kjeldsen Oct 29 '15 at 10:06
  • @FH-Inway, I was thinking in the same direction and tried with buf2buf(). As you sayed, the RecID then is 0. Unfortunately my problem was not solved because the problem was caused by another index during insert. – elToro Oct 29 '15 at 13:28

1 Answers1

4

The data method of table objects copies all fields including RecId and other system fields. The insert methods takes care of the generation of RecId.

By the above logic your code should not fail due to the RecId index.
Maybe another unique index is in play?
Most likely you passed a wrong or zero RecId in the call to the method.

You use two records buffer, this is unnecessary, just change the record key fields and insert:

while select translation 
    where translation.Category == this.RecId ...
{
    translation.Category = _duplicateComponent;
    translation.insert();
}
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
  • 1
    The problem is indeed coming from another index. So my assumption that the same RecId was causing the issue was wrong. – elToro Oct 29 '15 at 13:25