Saturday, August 9, 2025

Create default dimension using x++ D365fo

public DimensionDefault createDefaultDimension(str _businessUnit, str _centre, str _department)

{

    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();

    DimensionDefault                    result;

    int                                             i;

    DimensionAttribute                 dimensionAttribute;

    DimensionAttributeValue       dimensionAttributeValue;

    str                                            dimValue;


    // Define the list of dimension attribute names and their corresponding values

    container conAttr  = ["BusinessUnit" "Centre", "Department"];

    container conValue = [_businessUnit, _centre, _department];


    // Loop through each dimension to find the attribute and value

    for (i = 1; i <= conLen(conAttr); i++)

    {

        // Get the DimensionAttribute based on name

        dimensionAttribute = DimensionAttribute::findByName(conPeek(conAttr, i));

        // Get the corresponding value

        dimValue = conPeek(conValue, i);

        // Skip if attribute doesn't exist

        if (dimensionAttribute.RecId == 0)

        {

            continue;

        }

        // Only process if a value is provided

        if (dimValue != '')

        {

            // Find the DimensionAttributeValue based on attribute and value

            dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,                          dimValue, false, true );

            // Add the attribute value to the storage

            valueSetStorage.addItem(dimensionAttributeValue);

        }

    }

    // Save the dimension value set and return the DimensionDefault reference

    result = valueSetStorage.save();

    return result;

}


Call API using x++ D365FO

 System.Net.HttpWebRequest    request; System.Net.HttpWebResponse   response; System.IO.Stream             dataStream; System.IO.StreamReade...