Skip to content

Loading templates crashes XrmToolBox when a template's associated table cannot be resolved #1

Description

@skfd

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions