Reputation: 1256
I'm writing an extension method to copy source entity attributes to a new target AttributesCollection object given the attribute list to copy. If the source entity does not contain any of the provided attributes, the target collection should contain a Null value for that attribute. This works for most attribute types other then currency/money. I get an error message, 'The currency cannot be null.' when I update the target entity with the target collection.
public static AttributeCollection CopyAttributesFrom(this AttributeCollection target, Entity source, string[] attrList, bool resetAttributeIfMissingFromSource = false)
{
foreach (var attr in attrList)
{
if (resetAttributeIfMissingFromSource && !source.Contains(attr))
{
target[attr] = null;
continue;
}
target[attr] = source[attr];
}
return target;
}
Can someone provide a consistent and common approach to do this without having to inject the data types somehow and check the type in the method?
Upvotes: 0
Views: 558
Reputation: 22846
You have a Money attribute in that entity, hence TransactionCurrencyId
attribute is added to the entity, which should be filled in with the default currency of the Org or user setting currency. This will be utilized by the platform for supporting multi-currency calculations.
When you are trying to mark it as null, the validation kicks in and throwing this exception, this is expected. Either check the scenario when it is trying to nullify that attribute or keep it in a exclusion list from marking it as null.
Upvotes: 1