Book Image

Microsoft Dynamics AX 2012 Development Cookbook

By : Mindaugas Pocius
Book Image

Microsoft Dynamics AX 2012 Development Cookbook

By: Mindaugas Pocius

Overview of this book

Microsoft Dynamics AX is a comprehensive Enterprise Resource Planning (ERP) solution for mid-size and large organizations. Dynamics AX implementations are used worldwide by thousands of customers. With the new version - Dynamics AX 2012 - the system is due to expand even more rapidly. Every new implementation requires some level of customization, and all organizations want this to be done to the highest standards using proven approaches. Written by one of the leading experts in Microsoft Dynamics AX, 'Microsoft Dynamics AX 2012 Development Cookbook' is packed with over 80 task-based and immediately reusable recipes that will help you manage your company's or customer's ERP information and operations efficiently, and solve your business process problems in an effective and quick way. This book focuses on commonly used custom modifications in major Dynamics AX modules. The recipes in this book cover various areas of Dynamics AX to help developers not only learn about programming, but also about the functional side of Dynamics AX. The practical recipes will also allow you to look at the development from the perspective of business processes. You will learn to enhance your user interface using various Dynamics AX UI elements and managing your data and functions will become easier.
Table of Contents (15 chapters)
Microsoft Dynamics AX 2012 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Copying a record


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 the table's data() method, the global buf2buf() function, and their differences. As an example, we will copy one of the existing ledger account records into a new one.

How to do it...

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

  1. 1. Open General ledger | Common | Main accounts, and find the account to be copied. In this example, we will use 211100:

  2. 2. Open the AOT, create a new job named MainAccountCopy with the following code, and run it:

    static void MainAccountCopy(Args _args)
    {
    MainAccount mainAccount1;
    MainAccount mainAccount2;
    mainAccount1 = MainAccount::findByMainAccountId('211100');
    ttsBegin;
    mainAccount2.data(mainAccount1);
    mainAccount2.MainAccountId = '211101';
    if (!mainAccount2.validateWrite())
    {
    throw Exception::Error;
    }
    mainAccount2.insert();
    ttsCommit;
    }
    
  3. 3. Open General ledger | Common | Main accounts again, and notice that there are two identical records now:

How it works...

In this recipe, we have two variables—mainAccount1 for original record and mainAccount2 for the new one. First, we will need to find the original record by calling findByMainAccountId() on the MainAccount table.

Next, we will copy it to the new one. Here, we will use the data() table member method, which copies all data fields from one variable to another.

After that, we will set a new ledger account number, which is a part of a unique table index and must be different.

Finally, we call the insert() method on the table, if validateWrite() is successful. In this way, we have created a new ledger account record, which is exactly the same as the existing one apart from the account number.

There's more...

As we saw before, the data() method copies all table fields, including system fields such as 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. However, this function may not work for copying records across companies. In this case, we can use another function called buf2Buf(). It is very similar to the table's data() method with one major difference. The buf2Buf() function 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() method, as it checks and copies each field individually.

In order to use the buf2Buf() function, the code of the MainAccountCopy job could be amended as follows:

static void MainAccountCopy(Args _args)
{
MainAccount mainAccount1;
MainAccount mainAccount2;
mainAccount1 = MainAccount::findByMainAccountId('211100');
ttsBegin;
buf2Buf(mainAccount1, mainAccount2);
mainAccount2.MainAccountId = '211101';
if (!mainAccount2.validateWrite())
{
throw Exception::Error;
}
mainAccount2.insert();
ttsCommit;
}