Most of you who are familiar with the application probably used the standard Rename function. This function allows us to rename the primary key of almost any record. It is irreplaceable if a record was saved by mistake or simply needs renaming. The function ensures data consistency, that is, all related records are renamed too. It can be accessed from the Record information form, which can be opened by selecting Record info from the right-click menu on any record:
When it comes to mass renaming, this function might be very time consuming as it would need to be run on every record. An alternative is to create a job that automatically runs through all required records and calls this function.
This recipe will explain how the record primary key can be renamed through code.
As an example, we will create a job that renames general ledger account 110110 by adding the letter C in front of it. You can use any other ledger account in this example.
Open AOT to create a new job called LedgerAccountRename, and enter the following code:
static void LedgerAccountRename(Args _args) { LedgerTable ledgerTable; ; select firstonly ledgerTable where ledgerTable.AccountNum == '110110'; if (ledgerTable.RecId) { ledgerTable.AccountNum = 'C' + ledgerTable.AccountNum; ledgerTable.renamePrimaryKey(); } }
Open General ledger | Chart of Accounts Details and find the account to be renamed:
Click Transactions to note existing transactions:
Now, run the job. To check if the renaming was successful and the account retained all its transactions and other related records, open General ledger | Chart of Accounts Details, and find the new account:
Click Transactions to see that existing transactions are still in place:
In this recipe, first we select the desired ledger account, that is, 110110. If the where
clause is removed, then in such a case, all ledger accounts would be updated to have C in front of each account.
Next, and finally, we call the table's renamePrimaryKey()
, which does the actual renaming. It finds all related records and updates them with the new account number. The operation might take a while depending on the volume of data, as the system has to update every related record.