How to: Update 1 financial dimension of ledger dimension in D365FO in X++

 I received a request to update the cost center financial dimension for the posted GL transactions for the fixed asset depreciation journal. There was an issue with master data, and the default financial dimensions were corrected after the journal was posted.  Below is the job that I created to update the FD on the Ledger dimension using the default dimension


public static void main(Args _args)

    {

        GeneralJournalAccountEntry          GeneralJournalAccountEntryUpd;

        GeneralJournalEntry                 GeneralJournalEntryUpd;

        AssetTrans                          assetTrans;

        AssetBook                           assetBook;

        DimensionAttribute                  dimAttr;

        DimensionAttributeValueSetStorage   defaultDimStorage;

        DimensionAttributeValue             dimAttrValue;

        DimensionAttributeValueSetStorage   newDimStorage;

        RecId                               newDimSetRecId;

        RecId                               mergedLedgerDim;

        str                                 dimValue;

        ttsBegin;

        while select forUpdate GeneralJournalAccountEntryUpd

            where  (GeneralJournalAccountEntryUpd.PostingType == LedgerPostingType::LedgerJournal)

            join GeneralJournalEntryUpd

            where GeneralJournalEntryUpd.RecId          ==  GeneralJournalAccountEntryUpd.GeneralJournalEntry

&& GeneralJournalEntryUpd.SubledgerVoucherDataAreaId == "US01"

&&  GeneralJournalEntryUpd.SubledgerVoucher like "DPL-00*"

        {

            select firstonly assetTrans where assetTrans.Voucher == GeneralJournalEntryUpd.SubledgerVoucher && assetTrans.DataAreaId == GeneralJournalEntryUpd.SubledgerVoucherDataAreaId;

            select firstonly assetBook where assetBook.AssetId == assetTrans.AssetId  && assetBook.DataAreaId == GeneralJournalEntryUpd.SubledgerVoucherDataAreaId ;

            dimAttr = DimensionAttribute::findByName('CostCenter');

            defaultDimStorage = DimensionAttributeValueSetStorage::find(assetBook.DefaultDimension);

            dimValue = defaultDimStorage.getDisplayValueByDimensionAttribute(dimAttr.Recid);

            dimAttrValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttr, dimValue, false, true);

            newDimStorage = new DimensionAttributeValueSetStorage();

            newDimStorage.addItem(dimAttrValue);

            newDimSetRecId = newDimStorage.save();

            mergedLedgerDim = LedgerDimensionFacade::serviceCreateLedgerDimForDefaultDim(newDimSetRecId,GeneralJournalAccountEntryUpd.LedgerDimension);

            GeneralJournalAccountEntryUpd.LedgerDimension = mergedLedgerDim;

            GeneralJournalAccountEntryUpd.LedgerAccount   = DimensionAttributeValueCombination::find(GeneralJournalAccountEntryUpd.LedgerDimension).DisplayValue;

            GeneralJournalAccountEntryUpd.MainAccount   = DimensionAttributeValueCombination::find(GeneralJournalAccountEntryUpd.LedgerDimension).MainAccount;

            GeneralJournalAccountEntryUpd.DoUpdate();

            info(strFmt("%1",GeneralJournalAccountEntryUpd.RecId));

        }

        ttscommit;

    }

Hope this helps someone.



Comments

Popular posts from this blog

How to: Use enum values instead of integers in BI reports for Dynamics 365 finance and operations

How to: Import Bank statements from Azure file storage to Dynamics 365 finance and operation using Power automate (formally flow)

How to : get the Deep URL for a particular form with filter in Dynamics 365 Finance and Operations