Tuesday, September 19, 2023

How to create a new custom financial dimension value with description and other parameters via X++

User can add values manually to Custom dimension attribute type only. For all other backing entities it should go via standard table creation, say, new CustTable record, etc.



The easiest way to create a new Custom list dimension value is to use the standard service DimensionValueService as follows. Say, you need to create a new value by using _newProjCategory record fields.

DimensionValueService dimensionValueService = new DimensionValueService();
DimensionValueContract dimensionValueContract = new DimensionValueContract();
dimensionValueContract.parmValue(_newProjCategory.Id);
dimensionValueContract.parmDimensionAttribute(myDimHelper::getProjCategoryAttribute);
dimensionValueContract.parmDescription(_newProjCategory.Name);

dimensionValueService.createDimensionValue(dimensionValueContract);

It creates the display value as its description.





 

Saturday, September 2, 2023

Ledger dimension vs Default dimension

 In short Ledger dimension it is Main account + default dimension.

So, if you need to replace any attribute value in a given Ledger dimension, do not forget to get a Default dimension first.

LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(ledgerjournalTrans.LedgerDimension);

Then you can use this old good way.

DimensionDefault newDim = DimensionHelper::setValueToDefaultDimension(hcmEmployment.DefaultDimension, DimensionAttribute::findByName(_dimensionName).RecId, _dimensionValue);
        

BTW, the opposite thing works like that:

LedgerDimensionFacade::serviceCreateLedgerDimension(ledgerDimensionMainAccount, inventTrans.defaultDimension);
Supporting method
public static DimensionDefault setValueToDefaultDimension(DimensionDefault _dimensionDefault, RefRecId _dimensionAttributeRecId, DimensionValue  _newDimensionValue)
    {
        DimensionAttributeValueSetStorage   dimStorage;
        DimensionDefault                    newDimensionDefault = _dimensionDefault;
        DimensionAttributeValue             dimensionAttributeValue;
        if (_dimensionAttributeRecId)
        {
            dimStorage = DimensionAttributeValueSetStorage::find(_dimensionDefault);
            if (_newDimensionValue)
            {
                dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::find(_dimensionAttributeRecId), _newDimensionValue, false, true);
                dimStorage.addItem(dimensionAttributeValue);
            }
            else
            {
                dimStorage.removeDimensionAttribute(_dimensionAttributeRecId);
            }
            newDimensionDefault = dimStorage.save();
        }
        return newDimensionDefault;
    }

Bigger usage example

        // replace TransactionType to the one from parameters
        // first we need to get main account and default dimension from the originl transaction ledger dimension 
        mainAccountRecId = LedgerDimensionFacade::getMainAccountRecIdFromLedgerDimension(_ledgerJournalTransOrig.LedgerDimension);
        dimensionDefault = LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(_ledgerJournalTransOrig.LedgerDimension);
        // then replace this attribute with a new value
        dimensionDefault = myDimValueHelper::setDefaultDimensionValue(dimensionDefault, myDimensionConstants::TransactionType, LedgerParameters::find().myGLDimInvTransSale);
        // get default dimension for main account
        ledgerDimensionMainAccount = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId(mainAccountRecId);
        // finally combine it with original main account to get a new ledger dimension
        ledgerJournalTrans.OffsetLedgerDimension  = LedgerDimensionFacade::serviceCreateLedgerDimension(ledgerDimensionMainAccount, dimensionDefault);
        ledgerJournalTrans.modifiedField(fieldNum(LedgerJournalTrans, OffsetLedgerDimension));

 https://alexvoy.blogspot.com/2015/10/how-to-lookup-and-set-new-value-for.html

Thanks Sasha Nazarov and Ievgen Miroshnikov and Denis Trunin