Thursday, April 25, 2024

Update only cost center from given ledger dimension using X++ in D365 F&O

This code snippet allows you to change certain dimension values as needed and provides you with the updated ledger dimension.


MainAccount mainAccount= LedgerDimensionFacade::getMainAccountFromLedgerDimension(_accountingDistribution.LedgerDimension);
                    DimensionDefault defaultDimension = LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(_accountingDistribution.LedgerDimension);
                    defaultDimension = this.mergeCostCenterWithDefaultDimension(defaultDimension, inventSite.CostCenter);
                    LedgerDimensionAccount ledgerDimensionAccount2 = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId(mainAccount.RecId);

                    _accountingDistribution.LedgerDimension = LedgerDimensionFacade::ServiceCreateLedgerDimension(ledgerDimensionAccount2, defaultDimension);

public DimensionDefault MergeCostCenterWithDefaultDimension(DimensionDefault _defaultDimension, DimensionValue _costCenter)
    {
        DimensionAttribute                          dimAttribute;
        DimensionAttributeValue                     dimAttributeValue;
        DimensionAttributeValueSetStorage           dimStorage = new DimensionAttributeValueSetStorage();
        const str                                   costCenter = '@JP:Costcenter';
        DimensionDefault                            defaulDimension;

        if (_costCenter)
        {
            dimStorage          = DimensionAttributeValueSetStorage::find(_defaultDimension);
            dimAttribute        = DimensionAttribute::findByName(costCenter);
            dimAttributeValue   = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, _costCenter, true, true);
            dimStorage.addItem(dimAttributeValue);
            defaulDimension = dimStorage.save();
        }
        return defaulDimension;
    }

No comments:

Post a Comment