Data migration and integration are critical aspects of any business application, and Microsoft Dynamics 365 Finance and Operations (D365FO) offers a powerful toolset to facilitate these tasks. One of the key features for importing data is the Data Management framework (DMF). In this blog post, we'll explore how to use X++ code to import files using the Data Management framework in D365FO. This code is useful in scenarios where you want to call data entity import throgh integration and also by this code you dont need to worry about how many columns user is going to add later.
Background
The X++ code provided in this blog post demonstrates how to import files using the Data Management framework in D365FO. This code is encapsulated within the TestDataEntityImport
class and consists of several methods that work together to achieve a successful file import.
internal final class TestDataEntityImport
{
public static void main(Args _args)
{
FileUploadTemporaryStorageResult fileUpload;
AsciiStreamIo file;
System.IO.Stream stream;
fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
file = AsciiStreamIo::constructForRead(fileUpload.openResult());
stream = file.getStream();
TestDataEntityImport TestDataEntityImport = new TestDataEntityImport();
TestDataEntityImport.importFromBlob(stream, fileUpload.getFileName());
}
public DMFDefinitionGroup findDefinitionGroup(Filename _fileName)
{
DMFDefinitionGroup definitionGroup;
if (strStartsWith(_fileName, 'Test'))
{
select firstonly definitionGroup
where definitionGroup.DefinitionGroupName == 'TestVendorPlantImport';
//DMF import data project
}
return definitionGroup;
}
public DMFLocalFilePath applyTransforms(SharedServiceUnitFileID _uploadedStatement, DMFDefinitionGroup definitionGroup)
{
DMFDefinitionGroupEntity definitionGroupEntity = this.findDMFDefinitionGroupEntity(definitionGroup);
DMFExecutionId executionId = DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);
DMFDefinitionGroupExecution execution = DMFDefinitionGroupExecution::find(
definitionGroup.DefinitionGroupName,
definitionGroupEntity.Entity,
executionId,
true);
execution.IsTransformed = NoYes::No;
DMFLocalFilePath filePath = execution.applyTransforms(_uploadedStatement);
DMFExecution e = DMFExecution::find(executionId, true);
e.delete();
return filePath;
}
public void importFromBlob(System.IO.Stream _memory, str _fileName)
{
SharedServiceUnitFileID fileId;
DMFDefinitionGroup definitionGroup;
DMFDefinitionGroupEntity definitionGroupEntity;
DMFExecutionId executionId;
DMFDefinitionGroupExecution execution;
// Should be used to get file into FileUploadTemporaryStorageResult object
FileUploadTemporaryStorageResult result
= File::SendFileToTempStore_GetResult(_memory, _fileName);
if (result && result.getUploadStatus())
{
fileId = result.getFileId();
definitionGroup = this.findDefinitionGroup(_fileName);
this.applyTransforms(fileId, definitionGroup);
definitionGroupEntity = this.findDMFDefinitionGroupEntity(definitionGroup);
executionId = DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);
// Find execution
execution = DMFDefinitionGroupExecution::find(
definitionGroup.DefinitionGroupName,
definitionGroupEntity.Entity,
executionId,
true
);
execution.FilePath = fileId;
execution.IsTransformed = NoYes::Yes;
execution.IsSelected = NoYes::Yes;
execution.ExecuteTargetStep = NoYes::Yes;
execution.update();
setPrefix(strFmt("@SYS73667", _fileName));
// Import the file via quick import DMF
DMFQuickImportExport::doPGImport(definitionGroup.DefinitionGroupName, executionId, true);
// Deletes file
result.deleteResult();
}
}
}
Getting Started
The main
method serves as the entry point for the file import process. Let's break down the code step by step:
- File Upload: The code begins by getting a file from the user using the
File::GetFileFromUser()
method. The file is uploaded and stored in temporary storage. - File Reading: The uploaded file is then read using an
AsciiStreamIo
object. This object provides methods to read data from the uploaded file. - Stream Handling: The
stream
variable is assigned the stream of the file data, which will be used to process the contents of the file.
Conclusion
In this blog post, we've explored how to use X++ code to import files using the Data Management framework in Dynamics 365 Finance and Operations. The provided code demonstrates the step-by-step process of uploading a file, identifying the appropriate DMF Definition Group and Entity, applying transformations, and executing the import.
The Data Management framework in D365FO simplifies data migration and integration tasks, enabling organizations to efficiently import and process large volumes of data. By leveraging the power of X++ and the DMF, businesses can ensure accurate and streamlined data management within their Dynamics 365 environment.
No comments:
Post a Comment