Search code examples
axaptadynamics-ax-2012x++

xRecord.data method generates record with identical RecId


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?


Solution

  • 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();
    }