Skip to content

Do not crash the host when a template names a table that is gone - #3

Closed
skfd wants to merge 1 commit into
masterfrom
fix/formatted-value-crash
Closed

Do not crash the host when a template names a table that is gone#3
skfd wants to merge 1 commit into
masterfrom
fix/formatted-value-crash

Conversation

@skfd

@skfd skfd commented Aug 15, 2026

Copy link
Copy Markdown
Member

Fixes #1. Branched off master, not off #2, so it stands alone and can be cherry-picked
upstream on its own — it touches one file and does not depend on anything else in this fork.

The bug

GetFormattedAttribValue guarded on one collection and indexed another
(Helper/entity.partial.cs):

if (null != entity && entity.Attributes.Contains(attributeLogicalName))
{
    return entity.FormattedValues[attributeLogicalName];   // <-- different collection
}

An attribute can be present with no formatted value beside it. When it is, the indexer
throws KeyNotFoundException, the catch turns that into
Error: Unable to load formatted value for associatedentitytypecode and rethrows.

associatedentitytypecode is exactly that attribute. Dataverse only sends 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>

DocumentTemplateEdit's constructor reads it unconditionally while building the list
(Helper/DocumentTemplate.partial.cs:22), so one such record fails the entire load. And
because that runs on a background callback where nothing catches it, XrmToolBox does not show
an error and carry on — it terminates:

Faulting application name: XrmToolBox.exe, version: 1.2026.8.75
Faulting module name: KERNELBASE.dll
Exception code: 0xc000041d

The fix

Guard on the collection actually being indexed:

if (null != entity && entity.FormattedValues.Contains(attributeLogicalName))

GetFormattedAttribValue then returns null for an unresolvable table, and the row renders
with an empty Associated Entity cell instead of taking the host down.

Who hits this

Not a synthetic case — any template whose associated table cannot be resolved in the current
organization:

  • a template imported from another organization that has a table this one does not
  • a template whose table was deleted afterwards
  • a template created through the SDK whose content customXml names a missing table

The last one is how I ran into it. Note it is the template content's customXml that
decides the association: the server derives associatedentitytypecode from it and overwrites
whatever you set, so moving a template between organizations is enough to produce the state.

Verification

Reproduced deliberately by seeding a template whose content is still bound to
cr543_opportunity, a table the test organization does not have, alongside six ordinary ones.
Against master that is the crash above. Screenshot of the grid loading cleanly with the fix
to follow on this PR.

Not included here

The -Broken switch that seeds the repro record is tooling and belongs with the other test
scripts in #2, not on this branch, which is deliberately kept to the one file.

GetFormattedAttribValue guarded on entity.Attributes but then indexed
entity.FormattedValues. An attribute can be present with no formatted value beside
it, and the indexer throws KeyNotFoundException when it is, which the catch turned
into "Error: Unable to load formatted value for <name>" and rethrew.

associatedentitytypecode is that attribute. Dataverse only sends a formatted value
for it when it can resolve the table, so a template whose table was deleted, or
that was imported from an organization that has one this one does not, comes back
as 'none' with nothing formatted. DocumentTemplateEdit reads it unconditionally
while building the list, so a single such record failed the whole load -- and
because that runs on a background callback, nothing caught it and XrmToolBox went
down with it, APPCRASH 0xc000041d rather than an error message.

Guarding on the collection actually being indexed returns null instead, and the row
renders with an empty Associated Entity cell.

Fixes #1

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 15, 2026 00:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents XrmToolBox from crashing when a documenttemplate record includes associatedentitytypecode (or other attributes) without a corresponding entry in Entity.FormattedValues, which can happen when the associated table no longer exists or can’t be resolved.

Changes:

  • Fixes GetFormattedAttribValue to guard against missing formatted values by checking FormattedValues before indexing it.
  • Adds an explanatory comment describing the real-world scenario that triggers the crash.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@skfd skfd closed this Aug 16, 2026
@skfd
skfd deleted the fix/formatted-value-crash branch August 16, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants