Skip to content

Instantly share code, notes, and snippets.

@beeporama
Last active April 20, 2017 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beeporama/0d128590cff5c1d007883f07ab4a9d2c to your computer and use it in GitHub Desktop.
Save beeporama/0d128590cff5c1d007883f07ab4a9d2c to your computer and use it in GitHub Desktop.
PutDimension - X++ dimension updater
/// <summary>
/// Generic dimension updater. Call this with the DefaultDimension of any table, and update that table’s DefaultDimension with the returned value.
///
/// Sample usage:
/// hcmEmployment.DefaultDimension = xczDimensionHandler::PutDimension(hcmEmployment.DefaultDimension, ‘Favorite_Color’, ’01’);
/// hcmEmployment.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction); // This may or may not be appropriate for your circumstances
/// hcmEmployment.update();
///
/// Based on sample code courtesy Steeve Gilbert.
/// </summary>
/// <param name = "_DimensionAttributeValueSet">Value in the "DefaultDimension" column of the linked table (i.e. HcmEmployee, etc.). Links to DimensionAttributeValueSet.RecId. You will need to update the calling table this came from.</param>
/// <param name="_attribute">Attribute (e.g. "Department").</param>
/// <param name="_value">Potential value for the Attribute (e.g. "950").</param>
public static RecId PutDimension (RecId _defaultDimension, str _attribute, str _value)
{
DimensionAttribute dimAttribute;
DimensionAttributeValue dimAttributeValue;
DimensionAttributeValueSetStorage dimStorage;
// Verify that the value passed for _attribute identifies a real dimension.
select firstonly dimAttribute
where (dimAttribute.Name == _attribute);
if (!dimAttribute)
{
throw error(“Invalid _attribute passed to PutDimension: ” + _attribute);
}
// Find or create a storage dimension that contains each dimension attribute and their value.
dimStorage = DimensionAttributeValueSetStorage::find(_defaultDimension);
// Update or delete the value within that storage dimension.
if (_value)
{
dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, _value, true, true);
dimStorage.addItem(dimAttributeValue);
}
else
{
//If there’s no value, that means we want to remove that Dimension Attribute
dimStorage.removeDimensionAttribute(dimAttribute.RecId);
}
return dimStorage.save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment