/ Microsoft / Create default dimension using x++

Create default dimension using x++

Learn, share and spread knowledge

Default dimensions are used very commonly in D365FO and DAX 2012. During development or customization of the system, it is very common that we often stuck at the time when we have to create default dimension using x++ and from our own dimension values. To solve this problem, I am sharing this code. You just have to alter dimension names, values, arguments and no. of arguments.

Note: I am using three arguments, inserting values to three dimensions and getting DimensionDefault value for it. You can do this for one dimension or as many dimensions as you want.

//Change the arguments (variable) names and assign names as per your requirements
//Change in the function as well
public DimensionDefault createDefaultDimension(str department, str purpose, str costCenter)
{
    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
    DimensionDefault                    result;
    int                     i;
    DimensionAttribute      dimensionAttribute;
    DimensionAttributeValue dimensionAttributeValue;
    
    //Change the dimension names. Use the dimension name which are open and active in the system
    //I have given Region, Purpose and Costcentre just for an example
    
    container               conAttr = ["Region", "Purpose", "Costcentre"];
    
    //Change the values which you want to set in dimensions. Use values which are open and active in the system
    //I have given the arguments of function as values for dimensions.
    
    //Dimension name    ->      dimension value
    
    //Region            ->      department
    //Purpose           ->      purpose
    //Costcentre        ->      costCenter
    
    container               conValue = [department, purpose, costCenter];
    
    str                     dimValue;
    
    for (i = 1; i <= conLen(conAttr); i++)
    {
        dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));
        if (dimensionAttribute.RecId == 0)
        {
            continue;
        }
        dimValue = conPeek(conValue,i);
        if (dimValue != "")
        {
            dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);
            valueSetStorage.addItem(dimensionAttributeValue);
        }
    }
    
    result = valueSetStorage.save();
    //It reutrns the value of type DimensionDefault
    return result;
}

So, in this way you can create default dimension using x++. Moreover, if this helps you, please Like, Comment and Share to help other people.

If you found any ambiguity or a better solution, please feel free to ask.

See also:

Github: https://github.com/moeenahmedsultan/msdynamics/blob/master/create default dimensions using x++

Website: Click here

Blog: Click here

YouTube: Click here

GitHub: Click here

Facebook: Click here

Do all things with love, passion and dedication. – Patrick Driessen

1 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

1 Comments

NevoiTech Blog

A blog by Moeen Ahmed SultanNevoiTech logo

To hire freelancer or get personal training

of

Microsoft Dynamics 365 for Finance & Operations

Call Us: +92 321 458 9595