Search icon CANCEL
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
Dynamics 365 for Finance and Operations Development Cookbook

You're reading from   Dynamics 365 for Finance and Operations Development Cookbook Recipes to explore forms, look-ups and different integrations like Power BI and MS Office for your business solutions

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781786468864
Length 480 pages
Edition 4th Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Deepak Agarwal Deepak Agarwal
Author Profile Icon Deepak Agarwal
Deepak Agarwal
Abhimanyu Singh Abhimanyu Singh
Author Profile Icon Abhimanyu Singh
Abhimanyu Singh
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Processing Data FREE CHAPTER 2. Working with Forms 3. Working with Data in Forms 4. Building Lookups 5. Processing Business Tasks 6. Data Management 7. Integration with Microsoft Office 8. Integration with Power BI 9. Integration with Services 10. Improving Development Efficiency and Performance

Copying a record

Copying existing data is one of the data manipulation tasks in Dynamics 365 for Finance and Operations. There are numerous places in the standard D365 application where users can create new data entries just by copying existing data and then modifying it. A few of the examples are the Copy button in Cost management | Inventory accounting | Costing versions and the Copy project button in Project management and accounting | Projects | All projects. Also, although the mentioned copying functionality might not be that straightforward, the idea is clear: the existing data is reused while creating new entries.

In this recipe, we will learn two ways to copy records in X++. We will discuss 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 record.

How to do it...

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

  1. Navigate to General ledger | Chart of accounts | Accounts | Main accounts and find the account to be copied. In this example, we will use 130100, as shown in the following screenshot:
  1. Create a Dynamics 365 for Operations Project, create a runnable class named MainAccountCopy with the following code snippet, and run it:
        class MainAccountCopy 
       {         
         /// <summary> 
         /// Runs the class with the specified arguments. 
         /// </summary> 
         /// <param name = "_args">The specified arguments.</param> 
         public static void main(Args _args) 
        { 
          MainAccount mainAccount1; 
          MainAccount mainAccount2; 
 
          mainAccount1 = MainAccount::findByMainAccountId( 
          '130100'); 
 
          ttsBegin; 
          mainAccount2.data(mainAccount1); 
          mainAccount2.MainAccountId = '130101'; 
          mainAccount2.Name += ' - copy'; 
 
          if (!mainAccount2.validateWrite()) 
         {  
           throw Exception::Error; 
         } 
          mainAccount2.insert(); 
 
          ttsCommit;         
        } 
 
       } 
  1. Navigate to General ledger | Chart of accounts | Accounts | Main accounts again and notice that there are two identical records now, as shown in the following screenshot:

How it works...

In this recipe, we have two variables: mainAccount1 for the original record and mainAccount2 for the new record. First, we find the original record by calling findMainAccountId() in the MainAccount table.

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

After that, we set a new ledger account number, which is a part of a unique table index.

Finally, we call insert() on the table if validateWrite() is successful. In this way, we create 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 the table fields, including system fields such as the record ID, company account, and created user. 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 the companies. In this case, we can use another function called buf2Buf(). This function is a global function and is located in the Global class, which you can find by navigating to AOT | Classes. The buf2Buf() function is very similar to the table's data() method with one major difference. The buf2Buf() function copies all the data fields excluding the system fields. The code in the function is as follows:

    static void buf2Buf( 
     Common _from, 
     Common _to, 
     TableScope _scope = TableScope::CurrentTableOnly) 
    {  
      DictTable   dictTable = new DictTable(_from.TableId); 
      FieldId     fieldId   = dictTable.fieldNext(0, _scope); 
 
      while (fieldId && ! isSysId(fieldId)) 
     { 
        _to.(fieldId)   = _from.(fieldId); 
        fieldId         = dictTable.fieldNext(fieldId, _scope); 
     } 
    } 

We can clearly see that during the copying process, all the table fields are traversed, but the system fields, such as RecId or dataAreaId, are excluded. The isSysId() helper function is used for this purpose.

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

    class MainAccountCopyBuf2Buf 
   {         
     /// <summary> 
     /// Runs the class with the specified arguments. 
     /// </summary> 
     /// <param name = "_args">The specified arguments.</param> 
      public static void main(Args _args) 
     { 
       MainAccount mainAccount1; 
       MainAccount mainAccount2; 
 
       mainAccount1 = MainAccount::findByMainAccountId('130100'); 
 
       ttsBegin; 
       buf2Buf(mainAccount1, mainAccount2); 
 
       mainAccount2.MainAccountId = '130102'; 
       mainAccount2.Name += ' - copy'; 
 
       if (!mainAccount2.validateWrite()) 
      { 
        throw Exception::Error; 
      } 
 
       mainAccount2.insert(); 
 
       ttsCommit;         
     } 
 
   } 
You have been reading a chapter from
Dynamics 365 for Finance and Operations Development Cookbook - Fourth Edition
Published in: Aug 2017
Publisher: Packt
ISBN-13: 9781786468864
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