Copying a record
One of the tasks often used when manipulating data is record copying. For various reasons, an existing record needs to be modified and saved as a new one. The most obvious example could be when a user requires a function that allows him or her to quickly duplicate records on any of the existing forms.
There are several ways of copying one record into another in X++. In this recipe, we will explain the usage of the table's data()
method, the global buf2buf()
function, and their differences. As an example, we will copy one of the existing ledger account records into a new one.
How to do it...
Carry out the following steps in order to complete this recipe:
1. Open General ledger | Common | Main accounts, and find the account to be copied. In this example, we will use 211100:
2. Open the AOT, create a new job named
MainAccountCopy
with the following code, and run it:static void MainAccountCopy(Args _args) { MainAccount mainAccount1; MainAccount mainAccount2; mainAccount1 = MainAccount::findByMainAccountId('211100'); ttsBegin; mainAccount2.data(mainAccount1); mainAccount2.MainAccountId = '211101'; if (!mainAccount2.validateWrite()) { throw Exception::Error; } mainAccount2.insert(); ttsCommit; }
3. Open General ledger | Common | Main accounts again, and notice that there are two identical records now:
How it works...
In this recipe, we have two variables—mainAccount1
for original record and mainAccount2
for the new one. First, we will need to find the original record by calling findByMainAccountId()
on the MainAccount table.
Next, we will copy it to the new one. Here, we will use the data()
table member method, which copies all data fields from one variable to another.
After that, we will set a new ledger account number, which is a part of a unique table index and must be different.
Finally, we call the insert()
method on the table, if validateWrite()
is successful. In this way, we have created a new ledger account record, which is exactly the same as the existing one apart from the account number.
There's more...
As we saw before, the data()
method copies all table fields, including system fields such as record ID, company account, created user, and so on. Most of the time, it is OK because when the new record is saved, the system fields are overwritten with the new values. However, this function may not work for copying records across companies. In this case, we can use another function called buf2Buf()
. It is very similar to the table's data()
method with one major difference. The buf2Buf()
function copies all data fields excluding the system ones. The code in the function is as follows:
static void buf2Buf(Common _from, Common _to) { DictTable dictTable = new DictTable(_from.TableId); FieldId fieldId = dictTable.fieldNext(0); while (fieldId && ! isSysId(fieldId)) { _to.(fieldId) = _from.(fieldId); fieldId = dictTable.fieldNext(fieldId); } }
We can clearly see that during the copying process, all the table fields are traversed, but the system fields are excluded. We can also see that this function is slower than the internal data()
method, as it checks and copies each field individually.
In order to use the buf2Buf()
function, the code of the MainAccountCopy
job could be amended as follows:
static void MainAccountCopy(Args _args) { MainAccount mainAccount1; MainAccount mainAccount2; mainAccount1 = MainAccount::findByMainAccountId('211100'); ttsBegin; buf2Buf(mainAccount1, mainAccount2); mainAccount2.MainAccountId = '211101'; if (!mainAccount2.validateWrite()) { throw Exception::Error; } mainAccount2.insert(); ttsCommit; }