Set Financial Dimension combination for Purchase line using X++

In this article we will see how to set financial dimension combination for a PurchLine using X++ code.

Please follow the code below to set the financial dimensions.

        PurchLine                           purchLine;
        DimensionAttributeValueSetStorage   dimensionAttributeValueSetStorage;
        DimensionAttributeValue             dimensionAttributeValue;
        DimensionAttribute                  dimensionAttribute;
        container                           financialDimensionName;
        container                           financialDimensionValue;
        int                                 i;

        financialDimensionName  = ['BusinessUnit', 'CostCenter', 'Department', 'ItemGroup', 'Project'];
        financialDimensionValue = ['001', '007', '022', 'AudioRM', '000002'];

        purchLine  =  PurchLine::find('000038', 1);

        dimensionAttributeValueSetStorage = new DimensionAttributeValueSetStorage();

        for (i = 1; i <= conLen(financialDimensionName); i++)
        {
            dimensionAttribute      = DimensionAttribute::findByName(conPeek(financialDimensionName, i));
            dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, conPeek(financialDimensionValue, i), false, true);
            
            if (dimensionAttributeValue)
            {
                dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
            }
        }

        ttsBegin;
        purchLine.selectForUpdate(true);
        purchLine.DefaultDimension = dimensionAttributeValueSetStorage.save();
        purchLine.update();
        ttsCommit;

You can use the same code for others like Sales line etc. just replace the PurchLine buffer with the SalesLine etc.

Thank you for reading this article hope you find it useful.

Omar Iqbal

Technical Consultant

omar_iqbal

Recent Posts

Create and Update Rows Using a Plugin in Microsoft Dynamics CE (CRM)

Microsoft Dynamics CRM is a powerful Customer Relationship Management (CRM) tool that helps businesses manage…

2 years ago

Assign record owner to specific Team using Power Automate flow in Microsoft Dynamics CE (CRM)

Today we will be going over the steps how to assign owner to specific Team…

2 years ago

Set lookup in Microsoft Dynamics CE (CRM) using JavaScript

Today we will be going over the steps how to set the value of a…

2 years ago

Create and post free text invoice using X++ code

In this article we will see how we can create and post free text invoice…

3 years ago

Sales order confirmation using X++ code

In this article we will be focusing on confirmation of a sales order using X++…

3 years ago

Purchase order confirmation using X++ code

In this article we will be focusing on confirmation of a purchase order using X++…

3 years ago