Friday, 15 September 2023

Merging ledger dimension and financial dimension with main account id in x++:

 

Merging ledger dimension and financial dimension with main account id  in x++: 

     /// <summary>

    /// To update mainAccounting financial and accounting distribution dimension

    /// </summary>

    public void updateMainAccountinFinancialandAccountingDistributionDimension()

    {

        PurchReqBusinessJustificationCodes reasonCodes;

        LedgerDimensionDefaultAccount      newLedgerDimension;

        LedgerDimensionBase                mergedLedgerDimension;

        DimensionAttributeValueSetStorage  dimStorage;

        DimensionAttribute                 dimAttribute;

        DimensionAttributeValue            dimAttributeValue;

        AccountingDistribution    accDistribution;

        RefRecId                           mainAccountRecId;


        ttsbegin;

        select firstonly reasonCodes

            where reasonCodes.RecId == this.BusinessJustification;


        select firstonly forupdate accDistribution

            where accDistribution.SourceDocumentLine == this.SourceDocumentLine;

        //Get the main accoubt recid

        mainAccountRecId                = MainAccount::findByMainAccountId(reasonCodes.CCBMainAccountId).RecId;

        // Create new ledger dimension with only main account id and other dimensions are empty

        newLedgerDimension              = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId(mainAccountRecId);

        // Merging new ledger dimension with old ledger dimension 

        mergedLedgerDimension           = LedgerDimensionFacade::serviceMergeLedgerDimensions(newLedgerDimension,accDistribution.LedgerDimension);

        // Update the mrgde ledger dimnesion to table

        accDistribution.LedgerDimension = mergedLedgerDimension;

        accDistribution.update();


        // Merge main account in financial dimension  

        dimStorage                      = DimensionAttributeValueSetStorage::find(this.DefaultDimension);

        dimAttribute                    = DimensionAttribute::findByName('MainAccount');

        dimAttributeValue               = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, reasonCodes.CCBMainAccountId, true, true);


        dimStorage.addItem(dimAttributeValue);

        if(dimStorage != null)

        {

            this.selectForUpdate(true);

            this.CCBLineToBeExpense = NoYes::Yes;

            this.DefaultDimension   = dimStorage.save();

            this.doUpdate();

        }

        ttscommit;

    }



// Get the main account from ledger dimension:

MainAccount                     mainAccount = LedgerDimensionFacade::getMainAccountFromLedgerDimension(projPosting.LedgerDimension);


Get the default dimension(financial dimension) from ledger dimension: 

DimensionDefault defaultDimension = DimensionAttributeValueSetStorage::getDefaultDimensionFromDimensionCombination(_accDistribution.LedgerDimension);


Get the dimension value from default dimension recid :

(Ex: get Cost center value from default dimension)

public static str getEquipmentCostCenter(DimensionDefault _defaultDimension)

    {

        DimensionAttributeValueSetStorage   dimensionAttributeValueSetStorage ;

        DimensionAttribute                  dimensionAttribute;

        DimensionValue                      dimensionValue;

        #define.DimensionName("@CCB:Costcenter")


        dimensionAttributeValueSetStorage   = dimensionAttributeValueSetStorage::find(_defaultDimension);

        dimensionAttribute                  = dimensionAttribute::findbyname(#DimensionName);

        dimensionValue                      = dimensionAttributeValueSetStorage.getDisplayValueByDimensionAttribute(dimensionAttribute.recId);


        return dimensionValue;

    }



No comments:

Post a Comment