What happens
Pressing Load Templates shows
---------------------------
Error: Unable to load formatted value for associatedentitytypecode
---------------------------
OK
---------------------------
and then XrmToolBox terminates. It is a real process crash, not a handled error:
Faulting application name: XrmToolBox.exe, version: 1.2026.8.75
Faulting module name: KERNELBASE.dll
Exception code: 0xc000041d
(0xc000041d is an unhandled exception during a callback, i.e. thrown on a worker/callback
path where nothing catches it.)
Cause
GetFormattedAttribValue guards on the wrong collection -- Helper/entity.partial.cs:32-47:
public static string GetFormattedAttribValue(this Entity entity, string attributeLogicalName)
{
try
{
if (null != entity && entity.Attributes.Contains(attributeLogicalName))
{
return entity.FormattedValues[attributeLogicalName]; // <-- different collection
}
return default(string);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Error: Unable to load formatted value for " + attributeLogicalName, ex);
}
}
It checks Attributes, then indexes FormattedValues. An attribute can be present with no
formatted value, and then the indexer throws KeyNotFoundException, which the catch converts
into the message above and rethrows.
associatedentitytypecode is exactly such an attribute. Dataverse only returns a formatted
value for it when it can resolve the table; when it cannot, the record comes back as
associatedentitytypecode raw='none' <NO FORMATTED VALUE>
and DocumentTemplateEdit's constructor calls it unconditionally
(Helper/DocumentTemplate.partial.cs:22), so one such record takes down the whole load.
How to reproduce
Any documenttemplate whose associated table is not resolvable in the current organization:
- a template imported from another organization whose table does not exist here
- a template whose table was deleted afterwards
- a template created through the SDK whose content customXml names a table this organization
does not have (how I ran into it)
Suggested fix
Guard on the collection actually being indexed:
if (null != entity && entity.FormattedValues.Contains(attributeLogicalName))
That returns null for an unresolvable table and the row renders with an empty
Associated Entity cell, instead of failing the load and taking the host down.
Worth considering separately: DocumentTemplateEdit also does
AssociatedEntityLogicalName = template.GetAttribValue<string>("associatedentitytypecode")
right below, which is fine today but only because the attribute really is a string despite
the name.
What happens
Pressing Load Templates shows
and then XrmToolBox terminates. It is a real process crash, not a handled error:
(
0xc000041dis an unhandled exception during a callback, i.e. thrown on a worker/callbackpath where nothing catches it.)
Cause
GetFormattedAttribValueguards on the wrong collection --Helper/entity.partial.cs:32-47:It checks
Attributes, then indexesFormattedValues. An attribute can be present with noformatted value, and then the indexer throws
KeyNotFoundException, which the catch convertsinto the message above and rethrows.
associatedentitytypecodeis exactly such an attribute. Dataverse only returns a formattedvalue for it when it can resolve the table; when it cannot, the record comes back as
and
DocumentTemplateEdit's constructor calls it unconditionally(
Helper/DocumentTemplate.partial.cs:22), so one such record takes down the whole load.How to reproduce
Any
documenttemplatewhose associated table is not resolvable in the current organization:does not have (how I ran into it)
Suggested fix
Guard on the collection actually being indexed:
That returns
nullfor an unresolvable table and the row renders with an emptyAssociated Entity cell, instead of failing the load and taking the host down.
Worth considering separately:
DocumentTemplateEditalso doesAssociatedEntityLogicalName = template.GetAttribValue<string>("associatedentitytypecode")right below, which is fine today but only because the attribute really is a string despite
the name.