Home / Questions / How did successfully update custom fields in both the Data Entity and its Target Table when standard mapping did not work in D365FO?
Explanatory Question

How did successfully update custom fields in both the Data Entity and its Target Table when standard mapping did not work in D365FO?

👁 2 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

You can fixe the issue by overriding the mapEntityToDataSource method in his Data Entity extension and calling next mapEntityToDataSource() as the first line inside the method. This allowed the standard DMF mapping to run before his custom logic. After that, he checked the database operation type and the correct data source, retrieved the target table buffer, and manually assigned the custom field value from the Data Entity to the target table:


public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx,
                                  DataEntityDataSourceRuntimeContext _dataSourceCtx)
{
    // Run standard mapping first
    next mapEntityToDataSource(_entityCtx, _dataSourceCtx);

    // Run custom code only for specific operations
    if (_entityCtx.getDatabaseOperation() == DataEntityDatabaseOperation::YourOperation)
    {
        // Execute logic only for the correct table source
        if (_dataSourceCtx.name() == dataEntityDataSourceStr(YourEntity, YourEntityDataSource))
        {
            // Get the target table buffer
            YourEntityDataSourceTable buffer = _dataSourceCtx.getBuffer();

            // Map custom field to target table
            buffer.TableCustomField = this.EntityCustomField;
        }
    }
}