Create or modify data source value using data entity import framework in Dynamics 365 F&O x++ code
In this blog, I'll demonstrate how to use the Data Entity Import framework to insert custom or calculated values during the import process—from source to target entity. We'll keep it short and straightforward, so let’s dive right into the code.
Client Requirement
During the data import process into Dynamics 365 Finance and Operations, there is a need to compute derived values based on the source file. Specifically, the requirement involves:
Solution
For this purpose, you can use mapEntityToDatasource() method in the data entity.
Code:
[ExtensionOf(tableStr(SalesOrderHeaderV3Entity))]
final class Demo_SalesOrderHeaderV3Entity_Extension
{
public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
{
switch (_dataSourceCtx.name())
{
case dataEntityDataSourceStr(SalesOrderHeaderV3Entity, SalesTable):
// Get source data entity buffer
SalesOrderHeaderV3Entity salesOrderHeaderV3Entity = _entityCtx.getEntityRecord();
// Get target entity buffer
SalesTable salesTable = _dataSourceCtx.getBuffer();
// This code works for specific legal entity
if (salesTable.DataAreaId == 'ABC')
{
str formattedOutput;
formattedOutput = DateTimeUtil::toFormattedStr(
salesOrderHeaderV3Entity.ServiceDate,
23, // mm/yyyy
DateDay::None,
DateSeparator::None,
DateMonth::Short,
DateSeparator::None,
DateYear::Digits2,
TimeSeparator::Colon,
TimeSeparator::Colon
);
formattedOutput = subStr(formattedOutput, 1, 5);
/* Setting the own value in customer reference field of salesTable while importing the data
*/
salesTable.CustomerRef = strFmt(
"%1 %2",
"Working Month-",
formattedOutput
);
}
break;
}
next mapEntityToDataSource(_entityCtx, _dataSourceCtx);
}
}
Happy Learning,
Syed Amir Ali.