In some data migration scenarios, it is necessary to maintain the original record metadata, such as CreatedDateTime, instead of relying on system-generated timestamps. By default, system fields are protected from direct modification, but there is a way to override this behavior in X++ using the overwriteSystemfields(true) method.
When importing historical data from legacy systems, it’s often important to preserve:
CreatedDateTime)ModifiedDateTime)Without this ability, newly inserted records will have system-generated values, which could lead to inconsistencies in reporting or auditing.
The following X++ example demonstrates how to override system fields when inserting data into the HcmWorkerActionCommentHistoryEntity entity:
public class HcmWorkerActionCommentHistoryEntity extends common
{
public boolean insertEntityDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
{
boolean ret = true;
if (_dataSourceCtx.name() == dataentitydatasourcestr(HcmWorkerActionCommentHistoryEntity, HcmWorkerActionComment))
{
HcmWorkerActionComment workerActionComment;
workerActionComment = _dataSourceCtx.getBuffer();
// Enable overwriting system fields
workerActionComment.overwriteSystemfields(true);
// Preserve original CreatedDateTime
workerActionComment.(fieldNum(HcmWorkerActionComment, CreatedDateTime)) = this.CommentCreationTime;
workerActionComment.doInsert();
_dataSourceCtx.setDataSaved(true);
this.mapDataSourceToEntity(_entityCtx, _dataSourceCtx);
}
else
{
ret = super(_entityCtx, _dataSourceCtx);
}
return ret;
}
}
HcmWorkerActionComment before proceeding.overwriteSystemfields(true); allows system fields to be manually set.CreatedDateTime field is explicitly set to this.CommentCreationTime.doInsert(); ensures that the record is committed with the specified values.By leveraging the overwriteSystemfields(true) method, you can maintain historical data integrity during migrations. This technique ensures that original timestamps and metadata remain intact, improving data accuracy and compliance in Dynamics 365 Finance & Operations.
Have you used this approach in your projects? Let me know in the comments!