A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Create decimal metafield in code and add to metaclass - scale not set

Vote:
 

Hi Guys,

I'm trying to create a metafield in code and add this to a metaclass, I've come up with the following code, which works, however the scale for the decimal isn't set, this is always zero.

Any ideas, am I doing this correectly?

Thanks, Mark

var metaClassContext = MetaClass.Load(OrderContext.MetaDataContext, metaClassName);

if (metaClassContext.MetaFields[metaFieldName] != null)
{
    return;
}

var metaField = MetaField.Load(MetaDataContext.Instance, metaFieldName);

if (metaField == null)
{
    metaField = MetaField.Create(MetaDataContext.Instance, metaClassNamespace, metaFieldName, metaFieldFriendlyName,
        metaFieldDescription, metatDataType, metaDataLength, allowNulls, false, false, false);

    if (metatDataType == MetaDataType.Decimal)
    {
        metaField.Attributes[MetaFieldAttributeConstants.MdpPrecisionAttributeName] = decimalPrecision.ToString(CultureInfo.InvariantCulture);
        metaField.Attributes[MetaFieldAttributeConstants.MdpScaleAttributeName] = decimalScale.ToString(CultureInfo.InvariantCulture);
    }
}
 
metaClassContext.AddField(metaField);
#112552
Oct 30, 2014 5:50
Vote:
 

Bugger... I've just worked this out...

Code is fine, the issue was because the MetaField already existed, i.e. this field was initially created without scale, I removed the field from the MetaClass and ran this again but didn't see it being updated, once I removed the MetaField form the MetaField table and ran the code again, everythign worked as expected.  So user error...  Anway I guess this code could be enhanced to check for the MetaField already existing and update that.

So hope this helps someone else...

#112554
Oct 30, 2014 6:04
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.