Tuesday, May 23, 2023

D365 F&O - Override System Fields on Data Entity Insert

 During data migration, you may find it necessary to overwrite system fields like 'createdBy' or 'createdDateTime'. In this case, my client has a custom table that stores user comments and relies on these two fields to give context to who and when the comment was created.  

1. Create two custom fields on the data entity

    1. CreatedByMigrated
    2. CreatedDateTimeMigrated

2. Update the Staging Table from the Data Entity so that the new fields are added to the staging table
3. Override (or extend) the insertEntityDatasource() method of your entity

public boolean insertEntityDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
    {
        boolean ret;
 
        ARWSpecSheetComments comments;
 
        if(_dataSourceCtx.name() == dataEntityDataSourceStr(ARWSpecSheetCommentsEntity, ARWSpecSheetComments))
        {
            comments = _dataSourceCtx.getBuffer() as ARWSpecSheetComments;
            comments.overwriteSystemfields(true);
            comments.(fieldNum(ARWSpecSheetComments, CreatedBy)) = this.CreatedByMigrated;
            comments.(fieldNum(ARWSpecSheetComments, CreatedDateTime)) = this.CreatedDateTimeMigrated;
            _dataSourceCtx.setBuffer(comments);
        }
   
        ret = super(_entityCtx, _dataSourceCtx);
   
        return ret;
    

3. When importing your entity in data management, make sure the 'createdByMigrated' fields are set in the source to staging mapping, and you will also need to uncheck 'Validate Field' in the target mapping.