Skip to content

Fixes #9144 - External File Attachment - Migrating files fails#9295

Open
dorianwoolger wants to merge 2 commits into
microsoft:mainfrom
dorianwoolger:main
Open

Fixes #9144 - External File Attachment - Migrating files fails#9295
dorianwoolger wants to merge 2 commits into
microsoft:mainfrom
dorianwoolger:main

Conversation

@dorianwoolger

@dorianwoolger dorianwoolger commented Jul 9, 2026

Copy link
Copy Markdown

What & why

Fixes a bug in the "DA External Storage Sync" migration (report 8752) where the shared Tenant Media record behind multiple Document Attachment rows could be deleted after the first attachment was migrated, causing subsequent attachments referencing the same media to fail.

DeleteFromInternalStorage now takes a pre-built Guid → reference count map and only physically deletes a Tenant Media record once its count reaches zero, i.e. once every Document Attachment referencing it has been migrated. The map is built with a single pass over Document Attachment (BuildMediaReferenceCounts) before each batch runs, rather than re-querying per record — Media fields can't be filtered or passed as parameters directly, so this avoids an O(n²) scan while staying correct across partial runs and Maximum Records to Process limits.

Linked work

Fixes #9144

How I validated this

  • [Y] I read the full diff and it contains only changes I intended.
  • [Y] I built the affected app(s) locally with no new analyzer warnings.
  • [Y] I ran the change in Business Central and confirmed it behaves as expected.
  • [Y]I added or updated tests for the new behavior, or explained below why none are needed.

What I tested and the outcome (required — be specific: scenarios, commands, screenshots for UI changes)

I started with a fresh Cronus company. With External file storage disabled.
I attached some documents to an un-posted invoice and posted it. Verified that the new attachments on the posted invoice and the posted receipt were sharing the same Tenant Media Record.
I then enabled External File storage and ran the Sync with Destination to external storage and action of Move.
Let the report run.
The report ran without an failed files.
Checked that all attachments migrated correctly and that nothing was left un migrated.
Existing test updated to include the new MediaReferenceCounts Dictionary.

Risk & compatibility

This change only affects the migration of document attachments from internal to external storage.

@dorianwoolger dorianwoolger requested a review from a team July 9, 2026 20:53
@github-actions github-actions Bot added AL: Apps (W1) Add-on apps for W1 From Fork Pull request is coming from a fork needs-approval Workflow runs require maintainer approval to start labels Jul 9, 2026
@dorianwoolger

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@duichwer

Copy link
Copy Markdown

I like the now used approach but wouldn't it be an alternative solution to simple clear the media Id on the the document attachment an then later use the platform capability to delete orphaned media ?

What is the advantage of deleting it right away?

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

Labels

AL: Apps (W1) Add-on apps for W1 From Fork Pull request is coming from a fork needs-approval Workflow runs require maintainer approval to start

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: External File Attachment - Migrating files fails for Tenant Media referenced more than once.

2 participants