Renaming the primary key in D365FO with X++ code
Usually, we used Record Info | Rename function on the form to modify primary key data. When it comes to mass renaming, this function might be very time-consuming as you need to run it on every record. An alternative way of doing this is to create a job that automatically runs through all the required records and calls this function automatically.
The below example will explain how the record's primary key can be renamed through the code. As an example, we will create a job that renames a customer account.
class CustdAccountRename
{
public static void main(Args _args)
{
CustTable custTable;
ttsBegin;
select firstOnly custTable
where custTable.AccountNum == '2022-0001';
if (custTable )
{
custTable.AccountNum = 'CUS-0001';
custTable.renamePrimaryKey();
}
ttsCommit;
}
}
Select class?CustAccountRename?and right-click and then select?Set as startup object. Execute the class by clicking?Start?in Visual Studio and check whether the renaming was successful, by navigating to?All customers?again and finding the new account. The new account should have retained all its transactions and other related records.
D365 Finance & Operations (F&O) Senior Technical Consultant | AI-Driven ERP Solutions Specialist | Microsoft Dynamics Expert
2 年Very informative!