Book Image

Microsoft Dynamics AX 2009 Development Cookbook

By : Mindaugas Pocius
Book Image

Microsoft Dynamics AX 2009 Development Cookbook

By: Mindaugas Pocius

Overview of this book

Microsoft Dynamics AX provides a comprehensive Enterprise Resource Planning (ERP) solution for mid-size and larger organizations. Dynamics AX implementations are used worldwide by thousands of customers. With the new version - Dynamics AX 2009 - 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. This step-by-step guide will help you manage your company'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. This book contains commonly used bits of code which were real-world tested in at least one successful Dynamics AX implementation. Many of the recipes were deployed in many implementations and even across several versions of Dynamics AX. The examples 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 book should help developers who already know the basics of Dynamics AX programming to step up to the next level. The recipes 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. Managing your data and functions will become easier. You will also get numerous development tips and tricks from a Dynamics AX development expert.
Table of Contents (12 chapters)
Microsoft Dynamics AX 2009 Development Cookbook
Credits
About the Author
About the Reviewers
Preface
Index

Copying a record


I've experienced that 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 table data() method, global buf2buf() function, and their differences. As an example, we will copy one of the existing customer records into a new one. Normally, copying a customer involves more data around the customer like customer contacts, bank accounts, printing settings, and similar, but for demonstration purposes, we will assume that our goal is only to copy the customer record itself without worrying about related data.

How to do it...

  1. Open Accounts receivable | Customer Details, and find the customer to be copied. In this example, we will use 1104:

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

    static void CustTableCopy(Args _args)
    
     {
         CustTable custTable1;
         CustTable custTable2;
         ;
     
         custTable1 = CustTable::find('1104');
     
         ttsbegin;
     
         custTable2.data(custTable1);
     
         custTable2.AccountNum = '1105';
     
         custTable2.PartyId = '';
         custTable2.PartyId = DirParty::createPartyFromCommon(
             custTable2).PartyId;
     
         if (!custTable2.validateWrite())
         {
             throw Exception::Error;
         }
     
         custTable2.insert();
     
         ttscommit;
     }
  3. Open Accounts receivable | Customer Details again, and notice that there two identical customer records now:

How it works...

In this recipe, we have two variables—custTable1 for original record and custTable2 for new one. First, we find the original record by calling find() on the CustTable table.

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

After that, we set a new customer account number and new address book ID (we have to clear it before). These two fields are part of unique table indexes, and the system would issue an error if one of them is already used.

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

There's more...

As we saw before, the data() method copies all table fields including system fields like 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. But this is not the case when copying records between different companies.

If we were to modify the previously created job to include the changecompany() statement, the job would not work. Company ID would still be copied from original record to the new one and would not be changed during the insert.

To solve similar problems, Dynamics AX provides another function called buf2buf(). It is very similar to the table's data() method with one major difference. buf2buf() 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(), as it checks and copies field-by-field.

Now that we have learned about this function, let's use it. Let's update a previous example to copy customer records from one Dynamics AX company to another. Do not forget that in practice, such copying would not make any sense as much customer related data will not be copied, but here it will be used as a good example. Update the previous job to (replace TST with your company):

static void CustTableCopy(Args _args)
 {
     CustTable custTable1;
     CustTable custTable2;
     ;
 
     custTable1 = CustTable::find('1104');
 
     changecompany('TST')
     {
 
         ttsbegin;
 
         buf2buf(custTable1, custTable2);
 
         custTable2.AccountNum = '1105';
 
         custTable2.PartyId    = '';
         custTable2.PartyId = DirParty::createPartyFromCommon(
             custTable2).PartyId;
 
         if (!custTable2.validateWrite())
         {
             throw Exception::Error;
         }
 
         custTable2.insert();
 
         ttscommit;
         
     }
 }

There are only two differences here. First we change the current company before we start creating a new record. Second, we replace data() with buf2buf(). The latter function accepts two records as arguments—source and destination. When we run this job, a new record will be created in another company.