D365FO – AX – Update Data Entity Target Entity fields with X++

Imagine you want to set the value of a field of the Target Entity of a Data Entity with X ++ code

You have to create an extension class that extends the data Entity, extends “mapEntityToDataSource” method and add your code inside.

[ExtensionOf(tableStr(PurchPurchaseOrderHeaderV2Entity))] //Extends Data Entity
final class Al0PurchPurchaseOrderHeaderV2Entity_Extension
{
    public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx) //Extends method
    {
        switch (_dataSourceCtx.name())
        {
            case dataEntityDataSourceStr(PurchPurchaseOrderHeaderV2Entity, PurchTable):
            PurchPurchaseOrderHeaderV2Entity purchPurchaseOrderHeaderV2Entity = _entityCtx.getEntityRecord(); //Get Data Entity buffer (Source Entity)
            PurchTable purchTable = _dataSourceCtx.getBuffer(); //Get Target Entity buffer
            purchTable.MatchingAgreement = PurchAgreementHeader::findAgreementId(purchPurchaseOrderHeaderV2Entity.Al0PurchAgreementRef).RecId; //Set Target entity field 
         
            break;
        }
        
        next mapEntityToDataSource(_entityCtx, _dataSourceCtx);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s