Monday, May 21, 2018

How to generate the default dimension with the combination of dimension values using X++

Method 1 :-
static void DefaultDimension(Args _args)
{
DimensionAttributeValueSetStorage valueSetStorage = new DimensionAttributeValueSetStorage();
DimensionDefault result;

int i;
DimensionAttribute dimensionAttribute;
DimensionAttributeValue dimensionAttributeValue;
container conAttr = ['Department','ProfitCenter' ,'Divisions'];
container conValue = ['CE', 'HO', 'GL'];
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 != "")
{
// The last parameter is "true". A dimensionAttributeValue record will be created if not found.
dimensionAttributeValue =
dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);

// Add the dimensionAttibuteValue to the default dimension
valueSetStorage.addItem(dimensionAttributeValue);
info("%1",dimensionAttributeValue);
}
}

result = valueSetStorage.save();
info("%1",result);
}
--------------------------------------------------------------------------------------------------------------------------

Method 2

 static void DefaultDimension(Args _args)
 {

    container   varContainer;
    Counter     varCounter=0;
    RecId       retValueOfDefDimension;
    ;
    
    varContainer = [0];
    
    //assigning one dimension
    varContainer += ["Department","025"];
    varCounter++;//increase on assignmnet of each dimension
    
    //assigning 2nd dimension
    varContainer += ["CostCenter","010"];
    varCounter++;//increase on assignmnet of each dimension
    
    //so on.. you can pass any number of dimensions
    
    //prepare container
    varContainer = conPoke(varContainer,1,varCounter);
    
    //pass continer and get back value generated
    retValueOfDefDimension = AxdDimensionUtil::getDimensionAttributeValueSetId(varContainer);
    
    //display just for demo puprose. in real practice simply assign it to table field(s)
    if(retValueOfDefDimension)
        info(strFmt('You can verify record with RecId %1 in Table DimensionAttributeValueSetItem by filtering on field DimensionAttributeValueSet.',retValueOfDefDimension));    
}

No comments:

Post a Comment

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// &l...