Tuesday, November 21, 2023

Create financial Dimension using x++ in D365FO

For creating Financial Dimensions we have 2 ways.


 container offsetConLoc  =   [_mainaccount, #empty, department, costcenter, #empty, project];
 container offsetConLoc  =   [_mainaccount,_businessunit,_site, region, department,_Vendor, #empty, #empty, _fixedasset];

Way 1:

    public DimensionDynamicAccount   generateLedgerDimension(container    _conData,   MainAccountNum  mainAccountNum)
    {
        int                                  hierarchyCount;
        int                                  hierarchyIdx;
        RecId                                dimAttId_MainAccount;
        LedgerRecId                          ledgerRecId;
        MainAccount                          mainAccount;
        RefRecId                             recordvalue;
        DimensionAttribute                   dimensionAttribute;
        DimensionAttributeValue              dimensionAttributeValue;
        DimensionSetSegmentName              DimensionSet;
        DimensionStorage                     dimStorage;
        LedgerAccountContract                LedgerAccountContract = new LedgerAccountContract();
        DimensionAttributeValueContract      ValueContract;
        List                                 valueContracts = new List(Types::Class);
        dimensionAttributeValueCombination   dimensionAttributeValueCombination;
 

        mainAccount     =  MainAccount::findByMainAccountId(_mainAccountNum);
        recordvalue     =  DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
        hierarchyCount  =  DimensionHierarchy::getLevelCount(recordvalue);
        DimensionSet    =  DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);

 

        for(hierarchyIdx = 1;hierarchyIdx<=hierarchyCount;hierarchyIdx++)
        {
            if(hierarchyIdx == 1)
            {
                continue;
            }
            dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx],false, #enus);

 

            if(dimensionAttribute)
            {
                dimensionAttributeValue =DimensionAttributeValue::findByDimensionAttributeAndValue(
                                                    dimensionAttribute,conPeek(_conData,hierarchyIdx));

                if(dimensionAttributeValue)
                {
                    ValueContract = new DimensionAttributeValueContract();
                    ValueContract.parmName(dimensionAttribute.Name) ;
                    ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
                    valueContracts.addEnd(ValueContract);
                }
            }
        }
        LedgerAccountContract.parmMainAccount(_mainAccountNum);
        LedgerAccountContract.parmValues(valueContracts);
        dimStorage                          =    DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
        dimensionAttributeValueCombination  =    DimensionAttributeValueCombination::find(dimStorage.save());
        ledgerRecId                         =    dimensionAttributeValueCombination.RecId;

 

        return ledgerRecId;
    }


Way 2:

    public static void main(Args _args)
    {
	Class::generateLedgerDimension('110110', 
					'011', 
					'001', 
					'C-000002');
    }
	
    public static DimensionDynamicAccount generateLedgerDimension(
		MainAccountNum _mainAccount,
        str _department,
        str _businessUnit,
        str _customer)
    {
        DimensionAttributeValueSetStorage dimensionAttributeValueSetStorage 
            = new DimensionAttributeValueSetStorage();

        void addDimensionAttributeValue(
            DimensionAttribute _dimensionAttribute, 
            str _dimValueStr)
        {
            DimensionAttributeValue dimensionAttributeValue;

            if (_dimValueStr)
            {
                dimensionAttributeValue = 
                    DimensionAttributeValue::findByDimensionAttributeAndValueNoError(
                        _dimensionAttribute,
                        _dimValueStr);
            }

            if (dimensionAttributeValue.RecId != 0)
            {
                dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
            }
        }

        DimensionAttribute dimensionAttribute;

        while select dimensionAttribute
            where dimensionAttribute.ViewName == tableStr(DimAttributeOMDepartment)
               || dimensionAttribute.ViewName == tableStr(DimAttributeOMBusinessUnit)
               || dimensionAttribute.ViewName == tableStr(DimAttributeCustTable)
        {
            switch (dimensionAttribute.ViewName)
            {
                case tableStr(DimAttributeOMDepartment):
                    addDimensionAttributeValue(dimensionAttribute, _departmentId);
                    break;

                case tableStr(DimAttributeOMBusinessUnit):
                    addDimensionAttributeValue(dimensionAttribute, _businessUnit);
                    break;

                case tableStr(DimAttributeCustTable):
                    addDimensionAttributeValue(dimensionAttribute, _customer);
                    break;
            }            
        }

        RecId defaultDimensionRecId = dimensionAttributeValueSetStorage.save();

        return LedgerDimensionFacade::serviceCreateLedgerDimension(
            LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId(
                MainAccount::findByMainAccountId(_mainAccount).RecId),
            defaultDimensionRecId);
    }


Merge Main account and ledger dimensions:

LedgerDimensionFacade::serviceMergeLedgerDimensions(_mainAccountLedgerDimensionRecid, _ledgerDimension)

Keep Daxing!!






No comments:

Post a Comment