Do not crash the host when a template names a table that is gone - #3
Closed
skfd wants to merge 1 commit into
Closed
Conversation
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>
There was a problem hiding this comment.
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
GetFormattedAttribValueto guard against missing formatted values by checkingFormattedValuesbefore 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1. Branched off
master, not off #2, so it stands alone and can be cherry-pickedupstream on its own — it touches one file and does not depend on anything else in this fork.
The bug
GetFormattedAttribValueguarded on one collection and indexed another(
Helper/entity.partial.cs):An attribute can be present with no formatted value beside it. When it is, the indexer
throws
KeyNotFoundException, the catch turns that intoError: Unable to load formatted value for associatedentitytypecodeand rethrows.associatedentitytypecodeis exactly that attribute. Dataverse only sends a formatted valuefor it when it can resolve the table. When it cannot, the record comes back as:
DocumentTemplateEdit's constructor reads it unconditionally while building the list(
Helper/DocumentTemplate.partial.cs:22), so one such record fails the entire load. Andbecause that runs on a background callback where nothing catches it, XrmToolBox does not show
an error and carry on — it terminates:
The fix
Guard on the collection actually being indexed:
GetFormattedAttribValuethen returnsnullfor an unresolvable table, and the row renderswith 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:
The last one is how I ran into it. Note it is the template content's customXml that
decides the association: the server derives
associatedentitytypecodefrom it and overwriteswhatever 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
masterthat is the crash above. Screenshot of the grid loading cleanly with the fixto follow on this PR.
Not included here
The
-Brokenswitch that seeds the repro record is tooling and belongs with the other testscripts in #2, not on this branch, which is deliberately kept to the one file.