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.
StepCreate 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;
        }
}

Notewe 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

Popular posts from this blog

Configuring and using the Excel Data Connector add-in in D365 F&O

Configuring Power BI in D365 Finance and Operation

How to Change report title in SSRS Report in D365 F&O