Monday, November 25, 2019

How to merge Default Dimensions for Masters using x++ in D365 FO

Most of the times we get a requirement where we need to merge the dimensions from two areas in a single master.

For e.g In Sales Order Line the dimensions get populated from header but as soon as we select the Item Id the default financial dimensions are updated from Item Master and overrides the Sales Line Dimensions.

In order to achieve this merging from code we can use the below code in a data entity :-

        SalesTable                                             salestablefetch;
        SalesLine                                              saleslineupdate;
        CustTable                                              custtable;
        Str1260                                                 dimensionvalue;
        InventTable                                           inventTable;
        InvFinancialDimensions                       dimension = new InvFinancialDimensions() ;
        DimensionDefault                     defaultDimension,itemDefaultDimension,custDefaultDimension;

            select salestablefetch where salestablefetch.SalesId == this.SalesOrderNumber;
            inventTable = InventTable::find(this.ItemNumber);
            custtable = CustTable::find(salestablefetch.CustAccount) ;
           itemDefaultDimension    =   InventTable::find(inventTable.ItemId).DefaultDimension;
           custDefaultDimension    =   CustTable::find(custtable.AccountNum).DefaultDimension;
        defaultDimension  = DimensionDefaultFacade::serviceMergeDefaultDimensions

        while select forupdate saleslineupdate
            where saleslineupdate.SalesId == this.SalesOrderNumber && saleslineupdate.ItemId ==                                                                            this.ItemNumber

                saleslineupdate.DefaultDimension =  defaultDimension;

How to get or update values from Account Structure into the Default Financial Dimensions using x++ in D365 FO

Most of the times in our D365 Fin Ops Projects we get a requirement to fetch the dimension values from the Standard Account Structure for value updation in the respective masters through a normal button click event or insert() event handler in Data Entity .

Using the below logic we can easily accomplish our goal of doing the same :-

class CalculateFinancialDimensions
    public  Str1260 calculatefinancialdim(CustTable _custtable)
        List                                             dimensionAttributeList;
        ListEnumerator                          listEnumerator;
        DimensionAttributeSetItem       dimAttrSetItem;
        DimensionAttribute                   dimAttr;
        DimensionEnumeration             dimensionSetId;
        DimensionService                      dimensionService;
        DimensionContract                    dimensionContract;
        AccountStructureContract         accountStructureContract;
        Str1260                                      dimvalue ;
        DefaultDimensionView             defaultDimensionViewitem;

        dimensionSetId = DimensionCache::getDimensionAttributeSetForLedger();

        dimensionService = new dimensionService();

        accountStructureContract = new AccountStructureContract();

        dimensionAttributeList = dimensionService.getDimensions(accountStructureContract);
        listEnumerator = dimensionAttributeList.getEnumerator();

        while (listEnumerator.moveNext())
            dimensionContract = listEnumerator.current();

            // The below select statement will fetch only Active Dimensions
            select dimAttr
            join RecId from dimAttrSetItem
            where dimAttrSetItem.DimensionAttribute == dimAttr.RecId &&
                dimAttrSetItem.DimensionAttributeSet == dimensionSetId
                && dimAttr.Name==dimensionContract.parmDimensionName();

            select firstonly defaultDimensionViewitem
                where defaultDimensionViewitem.DefaultDimension ==  _custtable.DefaultDimension
                    && defaultDimensionViewitem.Name ==dimAttr.Name;

            if (defaultDimensionViewitem.RecId)
                dimvalue += '-'+defaultDimensionViewitem.DisplayValue ;
                dimvalue += '-' ;
        return dimvalue;


How to reverse Free Text Invoice Voucher entries without Dialog

 Hey Folks ,  This blog post is in continuation of the previous post for Reversing Free Text Invoice Voucher entries with Dialog. Only diffe...