I've experienced that 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 table data()
method, global buf2buf()
function, and their differences. As an example, we will copy one of the existing customer records into a new one. Normally, copying a customer involves more data around the customer like customer contacts, bank accounts, printing settings, and similar, but for demonstration purposes, we will assume that our goal is only to copy the customer record itself without worrying about related data.
Open Accounts receivable | Customer Details, and find the customer to be copied. In this example, we will use 1104:
Open AOT, create a new job called CustTableCopy with the following code and run it:
static void CustTableCopy(Args _args) { CustTable custTable1; CustTable custTable2; ; custTable1 = CustTable::find('1104'); ttsbegin; custTable2.data(custTable1); custTable2.AccountNum = '1105'; custTable2.PartyId = ''; custTable2.PartyId = DirParty::createPartyFromCommon( custTable2).PartyId; if (!custTable2.validateWrite()) { throw Exception::Error; } custTable2.insert(); ttscommit; }
Open Accounts receivable | Customer Details again, and notice that there two identical customer records now:
In this recipe, we have two variables—custTable1
for original record and custTable2
for new one. First, we find the original record by calling find()
on the CustTable table.
Next, we copy it to the new one. Here, we use the data()
table member method, which copies all data fields from one variable to another.
After that, we set a new customer account number and new address book ID (we have to clear it before). These two fields are part of unique table indexes, and the system would issue an error if one of them is already used.
Finally, we call insert()
on the table, if validateWrite()
is successful. In this way, we have created a new customer record, which is exactly the same as the existing one apart from the two fields.
As we saw before, the data()
method copies all table fields including system fields like 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. But this is not the case when copying records between different companies.
If we were to modify the previously created job to include the changecompany()
statement, the job would not work. Company ID would still be copied from original record to the new one and would not be changed during the insert.
To solve similar problems, Dynamics AX provides another function called buf2buf()
. It is very similar to the table's data()
method with one major difference. buf2buf()
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()
, as it checks and copies field-by-field.
Now that we have learned about this function, let's use it. Let's update a previous example to copy customer records from one Dynamics AX company to another. Do not forget that in practice, such copying would not make any sense as much customer related data will not be copied, but here it will be used as a good example. Update the previous job to (replace TST with your company):
static void CustTableCopy(Args _args)
{
CustTable custTable1;
CustTable custTable2;
;
custTable1 = CustTable::find('1104');
changecompany('TST')
{
ttsbegin;
buf2buf(custTable1, custTable2);
custTable2.AccountNum = '1105';
custTable2.PartyId = '';
custTable2.PartyId = DirParty::createPartyFromCommon(
custTable2).PartyId;
if (!custTable2.validateWrite())
{
throw Exception::Error;
}
custTable2.insert();
ttscommit;
}
}
There are only two differences here. First we change the current company before we start creating a new record. Second, we replace data()
with buf2buf()
. The latter function accepts two records as arguments—source and destination. When we run this job, a new record will be created in another company.