Dynamics AX allow you tu rename the primary key of a table. This is an example, using the LedgerTable. Right Click > Record Info

The press the Rename Button and put the value that you want. AX renames the primary key and all the relations.

 

This is the Clean Way. Internally AX calls the method renamePrimaryKey(…). But sometimes the rename function is not available. THis is the case of the Invoice numbers. For example, we are going to change the invoice number in the Project modules (ProjInvoiceJour)

If we go to the table and look for the method renamePrimaryKey, this method is override. Look the code

/// <summary>
///    Does not function.
/// </summary>
/// <remarks>
///    This method should not be called. The primary key for invoices cannot be renamed. If this method is called, the following error message will be displayed: An invoice cannot be renamed.
/// </remarks>
public void renamePrimaryKey()
{
    throw error("@SYS67280");
}

Then, comment the throw error line and add super(); to call the parent method.

 

/// <summary>
///    Does not function.
/// </summary>
/// <remarks>
///    This method should not be called. The primary key for invoices cannot be renamed. If this method is called, the following error message will be displayed: An invoice cannot be renamed.
/// </remarks>
public void renamePrimaryKey()
{
    //  throw error("@SYS67280");
    super();
}

The way that we use to call this methods is with this job. Please, try to avoid using this in a Production System. Only for development or pre-production systems.

 

static void renamePrimaryKey(Args _args)
{
     ProjInvoiceJour projInvoiceJour;
     Common  common;
     FieldId fieldId;
     DictTable dictTable;
     DictField  dictField;
    ;

     projInvoiceJour = ProjInvoiceJour::find("OLDVALUE",str2date("OLDDATE",321));
     common = projInvoiceJour;


     dictTable = new SysDictTable(common.TableId);
     dictField = new SysDictField(dictTable.id(), dictTable.primaryKeyField());

     startLengthyOperation();
     fieldId = dictField.id();
     try
     {
      ttsbegin;


      // CC Start
      CCPrimaryKey::renamePrimaryKey(common, "NEWVALUE", fieldId);
      // CC End


      common.(fieldId) = "NEWVALUE";


      common.renamePrimaryKey();
      if (common.TableId == tablenum(UserInfo))
      {
       common.update();
      }
      ttscommit;
     }
     catch (Exception::Error)
     {
      ttsabort;
     }

}