From 56332d4b5ec283bb60d0db2071269a025cf3bb5a Mon Sep 17 00:00:00 2001 From: Fer Date: Sun, 29 Nov 2026 10:05:24 +0100 Subject: [PATCH] Attachment cleanup by document type --- evaluation/review-fixtures.json | 8 ++++- ...attachment-cleanup-by-document-type.bad.al | 12 +++++++ ...ttachment-cleanup-by-document-type.good.al | 12 +++++++ ...der-attachment-cleanup-by-document-type.md | 34 +++++++++++++++++++ .../skills/review/al-data-modeling-review.md | 5 +-- 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.bad.al create mode 100644 microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.good.al create mode 100644 microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md diff --git a/evaluation/review-fixtures.json b/evaluation/review-fixtures.json index cfcf6be8..a75a6534 100644 --- a/evaluation/review-fixtures.json +++ b/evaluation/review-fixtures.json @@ -58,10 +58,16 @@ "pictures-must-use-media-not-blob", "report-barcodes-must-use-barcode-module-and-production-font-name", "round-calculated-unit-prices-to-unit-amount-precision", + "scope-sales-header-attachment-cleanup-by-document-type", "table-design-must-match-bc-table-type-conventions", "tablerelation-field-length-must-match-related-field", "transferfields-mirrored-fields-must-match-type-and-length" - ] + ], + "articleOverrides": { + "scope-sales-header-attachment-cleanup-by-document-type": { + "context": "Target Business Central 29, worldwide. A sales quote and a sales order have the same No. and each has a document attachment." + } + } }, "error-handling": { "articles": [ diff --git a/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.bad.al b/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.bad.al new file mode 100644 index 00000000..b12a76e2 --- /dev/null +++ b/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.bad.al @@ -0,0 +1,12 @@ +// Demonstration only; do not run against live attachments. +codeunit 50680 "Sales Attach Cleanup Bad" +{ + procedure DeleteAttachmentsForSalesHeader(SalesHeader: Record "Sales Header") + var + DocumentAttachment: Record "Document Attachment"; + begin + DocumentAttachment.SetRange("Table ID", Database::"Sales Header"); + DocumentAttachment.SetRange("No.", SalesHeader."No."); + DocumentAttachment.DeleteAll(true); + end; +} diff --git a/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.good.al b/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.good.al new file mode 100644 index 00000000..3ffe267c --- /dev/null +++ b/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.good.al @@ -0,0 +1,12 @@ +// Demonstration only; do not run against live attachments. +codeunit 50681 "Sales Attach Cleanup Good" +{ + procedure DeleteAttachmentsForSalesHeader(SalesHeader: Record "Sales Header") + var + DocumentAttachmentMgmt: Codeunit "Document Attachment Mgmt"; + SalesHeaderRef: RecordRef; + begin + SalesHeaderRef.GetTable(SalesHeader); + DocumentAttachmentMgmt.DeleteAttachedDocuments(SalesHeaderRef); + end; +} diff --git a/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md b/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md new file mode 100644 index 00000000..c6e97223 --- /dev/null +++ b/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md @@ -0,0 +1,34 @@ +--- +bc-version: [29] +domain: data-modeling +keywords: [document-attachment, sales-header, document-type, document-number, deleteall, attachment-cleanup] +technologies: [al] +countries: [w1] +application-area: [all] +--- + +# Scope sales header attachment cleanup by document type + +## Description + +In the BC 29 source, a `Sales Header` is identified by `Document Type` and `No.`, and its `Document Attachment` records carry the table ID, document number, and attachment document type. A quote and an order can therefore have the same number without being the same owner. Custom cleanup filtered only by table ID and number can delete both owners' attachments. + +## Best Practice + +For cleanup intended for one sales header, pass its `RecordRef` to `Document Attachment Mgmt`.`DeleteAttachedDocuments`, which applies the owner filters before deleting. If custom filtering is necessary, include the matching document type as well as the table ID and number, and retain any additional filters required by the actual operation and extensions. Do not assume the same dimensions identify every other table's attachments. Deletion can also affect external storage through installed extensions; verify that lifecycle separately. + +See sample: [`scope-sales-header-attachment-cleanup-by-document-type.good.al`](scope-sales-header-attachment-cleanup-by-document-type.good.al). + +## Anti Pattern + +Custom code deletes `Document Attachment` records for one `Sales Header` using only `Table ID` and `No.`, without restricting `Document Type`. It can remove attachments for a different sales document with the same number. Do not flag a deliberately cross-type operation whose intended scope includes those documents, or code that already constrains the type. + +See sample: [`scope-sales-header-attachment-cleanup-by-document-type.bad.al`](scope-sales-header-attachment-cleanup-by-document-type.bad.al). + +## References + +- [BCApps: Sales Header primary key](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Layers/W1/BaseApp/Sales/Document/SalesHeader.Table.al). +- [BCApps: attachment identity and initialization](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Layers/W1/BaseApp/Foundation/Attachment/DocumentAttachment.Table.al). +- [BCApps: attachment management and owner filters](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Layers/W1/BaseApp/Foundation/Attachment/DocumentAttachmentMgmt.Codeunit.al). +- [BCApps: external storage deletion subscriber](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Apps/W1/External%20Storage%20-%20Document%20Attachments/app/src/DocumentAttachmentIntegration/DAExternalStorageImpl.Codeunit.al). +- [External attachment storage](https://learn.microsoft.com/dynamics365/business-central/across-store-document-attachments-externally): files can be outside the database; do not assume record deletion and external file deletion have identical effects. diff --git a/microsoft/skills/review/al-data-modeling-review.md b/microsoft/skills/review/al-data-modeling-review.md index 725a924e..31316b40 100644 --- a/microsoft/skills/review/al-data-modeling-review.md +++ b/microsoft/skills/review/al-data-modeling-review.md @@ -18,7 +18,7 @@ Reviews AL source changes against the `data-modeling` knowledge domain in BCQual An orchestrator invokes this skill with a `pr-diff`, `file-path`, or `folder-path`. Data-modeling findings are narrow by design — they apply when the review scope contains setup or master tables, their card pages, primary keys, number-series assignment, block enforcement, audit fields, document print/email/Post-and-Send actions, `Navigate` page subscribers, Report Selection registration or dispatch, price-calculation/price-source extensibility, code that reads or writes sales/purchase/service line price and amount fields, `TransferFields`-based posting-cascade field mirroring, `TableRelation` field-length design, barcode/report-layout font-provider usage, dimension wiring, journal-based posting-routine structure, Item Ledger Entry document-number lookups after a combined sales post, code that reads a `Blob` field's stream, or code that inserts or deletes master/reference records. -It also applies when a business-facing field or consumer derives unresolved-work or completion status from the existence or absence of queue, log, staging, or buffer records, or a changed cleanup path deletes records used by such a consumer. This includes consumer-only changes without schema edits. The skill returns `not-applicable` when none of those apply. +It also applies when a business-facing field or consumer derives unresolved-work or completion status from the existence or absence of queue, log, staging, or buffer records, when a changed cleanup path deletes records used by such a consumer, or when custom code cleans up sales header document attachments. This includes consumer-only changes without schema edits. The skill returns `not-applicable` when none of those apply. ## Source @@ -48,6 +48,7 @@ A file enters the candidate worklist when its `keywords` intersect the extracted The following targeted checks cover every current `data-modeling` article. Treat each as a candidate-selection cue: when the signal appears in changed code, add the named article to the worklist and evaluate it in Action. - A business-facing field or consumer derives unresolved-work or completion status from the existence or absence of queue, log, staging, or buffer records (for example, an `exist` FlowField or `IsEmpty` read), or a changed cleanup path deletes records used by such a consumer — `business-state-must-have-a-domain-owner`. +- Custom code deletes `Document Attachment` records for one `Sales Header`, or calls attachment management for that cleanup — `scope-sales-header-attachment-cleanup-by-document-type`. Do not infer a defect from an intentionally cross-type operation or a cleanup already constrained to the owner's document type. - A `* Setup` table or its page changes singleton structure, uses a nonblank or generated key, permits insert/delete, uses a List page, or does not ensure the blank-keyed row exists — `setup-table-is-a-singleton`. - A new field is typed `Media`, `MediaSet`, or `BLOB` and the field's caption/name suggests a picture or image — `pictures-must-use-media-not-blob`. - Code outside a test codeunit or a demo-data generator calls `WorkDate(NewDate)` (the assignment form, not a bare `WorkDate()` read) as part of logic whose purpose is unrelated to the work date itself — `code-must-not-change-workdate`. A test deliberately setting a date context, or a demo-data routine that saves, sets, and restores the work date to backdate the data it creates, is not this anti-pattern. @@ -109,7 +110,7 @@ Outcome selection: - `completed` — the skill evaluated every worklist item. - `no-knowledge` — no applicable data-modeling knowledge survived filtering. -- `not-applicable` — the diff touches no setup/master table, page, key, numbering, block-check, or audit-field surface, and no document print/email/Post-and-Send action, `Navigate` subscriber, Report Selection registration/dispatch, price-calculation/price-source extensibility point, sales/purchase/service line price or amount read/write, posting-cascade `TransferFields` mirroring, `TableRelation` field length, barcode/report-font-provider usage, dimension wiring, posting-routine structure, Item-Ledger-Entry-document-number surface, `Blob` stream read, master/reference-record insert/delete call, or the technical-record existence/cleanup dependency described in the opening scope (including consumer-only changes). +- `not-applicable` — the diff touches no setup/master table, page, key, numbering, block-check, or audit-field surface, and no document print/email/Post-and-Send action, `Navigate` subscriber, Report Selection registration/dispatch, price-calculation/price-source extensibility point, sales/purchase/service line price or amount read/write, posting-cascade `TransferFields` mirroring, `TableRelation` field length, barcode/report-font-provider usage, dimension wiring, posting-routine structure, Item-Ledger-Entry-document-number surface, `Blob` stream read, master/reference-record insert/delete call, sales-header attachment cleanup, or the technical-record existence/cleanup dependency described in the opening scope (including consumer-only changes). - `partial` — a budget was hit before the worklist was exhausted. - `failed` — an unrecoverable error occurred.