D365FO – AX – Generate an exception and Write detailed error message while import a Data Entity

This code describes how to generate an exception and write detail log error message while importing a Data Entity.

 
public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
{
//Retrieve purchTable.MatchingAgreement RecId to retrieve Purchase agreement during "PurchTable.Inserting" method
str errorTxt;
switch (_dataSourceCtx.name())
{
      case dataEntityDataSourceStr(PurchPurchaseOrderHeaderV2Entity, PurchTable):
        PurchPurchaseOrderHeaderV2Entity purchPurchaseOrderHeaderV2Entity = _entityCtx.getEntityRecord();
        PurchTable purchTable = _dataSourceCtx.getBuffer();
        if (purchPurchaseOrderHeaderV2Entity.Al0PurchAgreementRef != '')
        {
            PurchAgreementHeader purchAgreementHeader = PurchAgreementHeader::findAgreementId(purchPurchaseOrderHeaderV2Entity.Al0PurchAgreementRef);
            if(purchAgreementHeader)
            {
                if (purchAgreementHeader.VendAccount == purchPurchaseOrderHeaderV2Entity.OrderVendorAccountNumber)
                {
                    purchTable.MatchingAgreement = purchAgreementHeader.RecId;
                }
                else
                {
                    errorTxt = strFmt("@Al0AM:Al0_00247_EntityPurchOrderHeaderAddFld_02",purchPurchaseOrderHeaderV2Entity.Al0PurchAgreementRef, purchPurchaseOrderHeaderV2Entity.OrderVendorAccountNumber);
                }
            }
            else
            {
                errorTxt = strFmt("@Al0AM:Al0_00247_EntityPurchOrderHeaderAddFld_01",purchPurchaseOrderHeaderV2Entity.Al0PurchAgreementRef);
            }
        }
        if(purchTable.MatchingAgreement == 0)
        {
            throw Error(errorTxt); //This method generate an exception and write error message into Data Entity Message log
        }
        break;
    }
    next mapEntityToDataSource(_entityCtx, _dataSourceCtx);
    //Al0_00247_EntityPurchOrderHeaderAddFld - END
}

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