Thursday, April 4, 2019

D365FO Entity data populating from staging table to target table

Adding a custom field through extension in standard entity, you may encounter a problem where all fields are populated in staging table (including the custom field) but not in target table. The custom field will not make to be populated in main table after doing all necessary mapping. In this case you need to make post eventhandler of copyCustomStagingToTarget on entity.

In this example, a new field by name 'CustomField' on CustInvoiceLine table needs to be populated using FreeTestInvoiceEntity. The method execution will get 'CustomField' from staging table and update it in the target table.


/// <summary>
/// Populate custom field
/// </summary>
/// <param name="args"></param>
[PostHandlerFor(tableStr(FreeTextInvoiceEntity), tableStaticMethodStr(FreeTextInvoiceEntity, copyCustomStagingToTarget))]
 public static void FreeTextInvoiceEntity_Post_copyCustomStagingToTarget(XppPrePostArgs args)
 {
        DMFDefinitionGroupExecution                 _dmfDefinitionGroupExecution =  Args.getArg('_dmfDefinitionGroupExecution');
        FreeTextInvoiceStaging                      staging;
        CustInvoiceLine                             custInvoiceLine;
        CustInvoiceTable                            custInvoiceTable;
      
        custInvoiceLine.skipDataMethods(true);
        SelectableDataArea currentCompany = curExt();

        update_recordset custInvoiceLine setting
                CustomField = staging.CustomField
            join custInvoiceTable
                where custInvoiceTable.RecId == custInvoiceLine.ParentRecId
            join staging
                where staging.DefinitionGroup       == _dmfDefinitionGroupExecution.DefinitionGroup
                    && staging.ExecutionId              == _dmfDefinitionGroupExecution.ExecutionId
                    && staging.TransferStatus          == DMFTransferStatus::Completed
                    && staging.LineNumber             == custInvoiceLine.LineNum;
    }

No comments:

Post a Comment