Renaming of Primary Key
Renaming of Primary Key through Code in X++
Hi Everyone, This blog explain how to renaming of primary key through code in the Dynamics 365 for Finance and Operations.
Most of you who are familiar with the Dynamics 365 for Finance and Operations applications, have probably used the standard Rename function. This function allows you to
rename the primary key of almost any record. With this function, you can fix records that
were saved or created by mistake. This function ensures data consistency, that is, all the
related records are renamed as well.
In this blog I 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.
Step: Create a new project, create a runnable class named CustAccountRename, and
enter the following code snippet.
class CustAccountRename
{
public static void main(Args _args)
{
CustTable custTable;
ttsbegin;
select firstonly custTable
where custTable.CustAccount == "Lok";
if (custTable)
{
custTable.custAccount = "US-110";
custTable.renamePrimaryKey();
}
ttscommit;
}
}
Note: we call the table's renamePrimaryKey() method, which does the actual renaming.
The method finds all the related records for the selected customer account and updates them
with the new value. The operation might take a while, depending on the volume of data, as
the system has to update multiple records located in multiple tables.
Comments
Post a Comment