Monday, October 1, 2018

Copying a Record using data() method in D365 for Operations

Introduction

Data is an essential pillar of an Enterprise. Therefore, Microsoft Dynamics 365 for Finance and Operations provides various data manipulation tasks. Among which, copying an existing record for re usability is an important task. Dynamics 365 application provides numerous places where the copy functionality exists: Project management and accounting > Projects > All projects > Copy project or Cost management > Inventory accounting > Costing versions. The idea is clear: the existing data is reused while creating new entries.

Solution

Microsoft provides an out-of-the-box solution for our problem - The Common table provides us with a data() method that lets us copy all fields from a record buffer into another. This approach provides us with an independent copy that is capable of operating on its own.

Demonstration

In this demonstration we are going to duplicate a record on HcmEthnicOrigin form (Human resources > Setup > Ethnic origins). Lets take a look at the record that we want to duplicate:



We are going to use a Runnable class to duplicate the record shown above:
public static void main (Args _args) 

    HcmEthnicOrigin asianEthnicOrigin = HcmEthnicOrigin::findByEthnicOrigin('Asian'); 

    ttsBegin
    HcmEthnicOrigin arabEthnicOrigin = asianEthnicOrigin.data(); 

    arabEthnicOrigin.EthnicOriginId = 'Arab'
    arabEthnicOrigin.Description = 'A person having origins in Saudi Arab, Kuwait, Oman and UAE'

    if (!arabEthnicOrigin.validateWrite()) 
    { 
        throw Exception::Error; 
    } 

    arabEthnicOrigin.insert(); 
    ttsCommit

The result after running the class is (Arab record is inserted):


Conclusion

In this little demonstration we can see the power that lies within the data() method. It is capable of creating a standalone copy of a record that can be modified as per our requirement. This allows for a better development and user experience since the user is engaged in modifying fields as per his requirement.

Copying a Record using data() method in D365 for Operations

Introduction Data is an essential pillar of an Enterprise. Therefore, Microsoft Dynamics 365 for Finance and Operations provides various ...