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
}

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...