Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Dynamics AX 2012 Development Cookbook

You're reading from   Microsoft Dynamics AX 2012 Development Cookbook Customizing Dynamics AX to suit the specific needs of an organization is plain sailing when you use this cookbook of modifications. With more than 80 practical recipes it's the perfect handbook for all Dynamics AX developers.

Arrow left icon
Product type Paperback
Published in May 2012
Publisher Packt
ISBN-13 9781849684644
Length 372 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Mindaugas Pocius Mindaugas Pocius
Author Profile Icon Mindaugas Pocius
Mindaugas Pocius
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Microsoft Dynamics AX 2012 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
1. Preface
1. Processing Data FREE CHAPTER 2. Working with Forms 3. Working with Data in Forms 4. Building Lookups 5. Processing Business Tasks 6. Integration with Microsoft Office 7. Using Services 8. Improving Development Efficiency 9. Improving Dynamics AX Performance

Renaming the primary key


Most of you, who are familiar with the Dynamics AX application, have probably used the standard Rename function. This function allows us to rename the primary key of almost any record. It is irreplaceable if a record was saved by mistake or simply needs renaming. The function ensures data consistency that is, all related records are renamed too. It can be accessed from the Record information form (shown in the following screenshot), which can be opened by selecting Record info from the right-click menu on any record:

When it comes to manual mass renaming, this function might be very time-consuming. An alternative way of doing that is to create a job that automatically runs through all required records and calls this function automatically.

This recipe will explain how the record primary key can be renamed through the code. As an example, we will create a job that renames a customer account.

How to do it...

Carry out the following steps in order to complete this recipe:

  1. 1. Open Accounts receivable | Common | Customers | All customers and find the account that has to be renamed:

  2. 2. Click on Transactions in the action pane to check the existing transactions:

  3. 3. Open the AOT, create a new job named CustAccountRename, and enter the following code. Use the previously selected account:

    static void CustAccountRename(Args _args)
    {
    CustTable custTable;
    select firstOnly custTable
    where custTable.AccountNum == '1103';
    if (custTable.RecId)
    {
    custTable.AccountNum = '1103_';
    custTable.renamePrimaryKey();
    }
    }
    
  4. 4. Run the job and check if the renaming was successful, by navigating to Accounts receivable | Common | Customers | All customers again, and finding the new account. The new account should have retained all its transactions and other related records, as shown in the following screenshot:

  5. 5. Click on Transactions in the action pane in order to see if existing transactions are still in place:

How it works...

In this recipe, first we will select the desired customer account that is, 1103. Here we can easily modify the select statement to include more accounts for renaming, but for demonstration purposes, let's keep it simple. Note that only fields belonging to a table's primary key can be renamed in this way.

Then 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 account. The operation might take a while depending on the volume of data, as the system has to update multiple records located in multiple tables.

You have been reading a chapter from
Microsoft Dynamics AX 2012 Development Cookbook
Published in: May 2012
Publisher: Packt
ISBN-13: 9781849684644
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image