Add reference group type field in data entity for import and export in d365

 Add reference group type field in Data entity for import and export in d365

Sometime, we get requirement we need to add field in data entity which shows data in backend table as rec id and ID field in frontend.

Example, there is one custom field sales category on sales order header, which save data in table as recid but it shows as Id field in front end. 

1 > Add string data type field in sales order header V2 staging table extension.



2 > Add field in data entity SalesOrderHeaderV2Entity extension.



3 > Create extension class for table for SalesOrderHeaderV2Entity

A > for import, can make extension for method mapEntityToDataSource()

B > for export, can make extension for method postLoad()


[ExtensionOf(tableStr(SalesOrderHeaderV2Entity))]

internal final class SalesOrderHeaderV2Entity_Extension

{

    public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)

    {

        EcoResCategory ecoResCategory;

        switch (_dataSourceCtx.name())

        {

            case dataEntityDataSourceStr(SalesOrderHeaderV2Entity, SalesTable):

                SalesOrderHeaderV2Entity salesOrderHeaderV2Entity = _entityCtx.getEntityRecord(); //Get Data Entity buffer (Source Entity)

                SalesTable salesTable = _dataSourceCtx.getBuffer(); //Get Target Entity buffer


                select firstonly RecId from ecoResCategory

                    where ecoResCategory.Code == this.SalesCategoryCode;


                if (ecoResCategory)

                {

                    salesTable.SalesCategory = ecoResCategory.RecId; //Set Target entity field

                    select firstonly ecoResCategory

                    where ecoResCategory.RecId == salesTable.SalesCategory;


                    this.PaymentTermsName = ecoResCategory.CustPaymTermId;

                }

                break;

        }


        next mapEntityToDataSource(_entityCtx, _dataSourceCtx);

    }


    public void postLoad()

    {

        EcoResCategory ecoResCategory;


        next postLoad();


        RefRecId category = (select salesCategory from salesTable

                                            where SalesTable.SalesId == this.SalesOrderNumber).salesCategory;


        this.SalesCategoryCode  = (select Code from EcoResCategory where EcoResCategory.Recid == category).code;

        select firstonly ecoResCategory

                    where ecoResCategory.RecId == category;


        this.PaymentTermsName = ecoResCategory.CustPaymTermId;


    }


}








Comments

Popular posts from this blog

Electronic reporting : change vendor XML Vendor Payment File from Zipped folder to unzipped file.