Skip to content

fix(E-Document): loop all records in OnAfterSend and OnAfterSendToEMail subscribers#7785

Open
jeffreybulanadi wants to merge 4 commits intomicrosoft:mainfrom
jeffreybulanadi:fix/5650-edocument-onaftersend-loop-all-records
Open

fix(E-Document): loop all records in OnAfterSend and OnAfterSendToEMail subscribers#7785
jeffreybulanadi wants to merge 4 commits intomicrosoft:mainfrom
jeffreybulanadi:fix/5650-edocument-onaftersend-loop-all-records

Conversation

@jeffreybulanadi
Copy link
Copy Markdown

@jeffreybulanadi jeffreybulanadi commented Apr 21, 2026

Summary

When multiple posted documents are selected and sent using a Document Sending Profile configured for e-documents, only the first document was created as an e-document.

Root Cause

In EDocumentSubscribers.Codeunit.al, both OnAfterSendEDocument and OnAfterSendToEMailEDocument processed the incoming RecordVariant as a single record without iterating through all records in the set. The check and creation calls operated on the same fixed first record on every call.

Fix

Use RecordRef.GetTable + FindSet/Next to iterate over every record in the variant. In each iteration, the current RecordRef position is passed to IsEDocumentCreatedForRecord and CreateEDocumentFromPostedDocument so each document is independently checked and created.

For OnAfterSendToEMailEDocument, only the e-document creation loop is changed. The subsequent ProcessEDocumentAsEmail call continues to receive the original RecordVariant as it handles the email attachment step for the full selection.

Files Changed

  • src/Apps/W1/EDocument/App/src/Processing/EDocumentSubscribers.Codeunit.al

Fixes #5650

Fixes AB#632028

…il subscribers

When multiple posted documents are selected and sent using a Document
Sending Profile configured for e-documents, only the first document
was being created as an e-document. The root cause was that
OnAfterSendEDocument and OnAfterSendToEMailEDocument each processed
the incoming RecordVariant as a single record instead of iterating
through all records.

Use RecordRef.GetTable + FindSet/Next to iterate over every record
in the variant. Pass the RecordRef for each iteration to both
IsEDocumentCreatedForRecord and CreateEDocumentFromPostedDocument so
each record is independently checked and created.

Fixes microsoft#5650
@jeffreybulanadi jeffreybulanadi requested a review from a team as a code owner April 21, 2026 20:09
@github-actions github-actions Bot added AL: Apps (W1) Add-on apps for W1 From Fork Pull request is coming from a fork labels Apr 21, 2026
@Groenbech96
Copy link
Copy Markdown
Contributor

This change needs to have tests that cover the scenario.
Also the negative case that a single document, not multi selected, still creates a document.

@Groenbech96 Groenbech96 self-assigned this Apr 22, 2026
@Groenbech96 Groenbech96 added Linked Issue is linked to a Azure Boards work item Approved The issue is approved labels Apr 22, 2026
@github-actions github-actions Bot added this to the Version 29.0 milestone Apr 22, 2026
Add EDocSendSubscriberTest codeunit (ID 139897) with two test procedures
that exercise the RecordRef.FindSet/Next loop added in the fix:

- SendMultiplePostedInvoicesCreatesEDocumentForEach: posts two invoices
  without auto E-Document creation, then sends both together via
  DocumentSendingProfile.Send() and asserts EDocument.Count() = 2.
- SendSinglePostedInvoiceCreatesOneEDocument: regression guard ensuring
  a single-invoice send still produces exactly one E-Document.

Pattern follows EDocEmailTests: disable DSP before posting, re-enable
before sending, bind EDocImplState subscriber so the mock format produces
non-empty TempBlob content through the processing pipeline.

Closes microsoft#5650
@jeffreybulanadi
Copy link
Copy Markdown
Author

Thank you for the feedback! Both test scenarios are already included in the second commit of this PR (2514b3a).

New test file: src/Apps/W1/EDocument/Test/src/Processing/EDocSendSubscriberTest.Codeunit.al (codeunit 139897 "E-Doc. Send Subscriber Test")

Test 1 - Multi-document scenario (the bug case):
SendMultiplePostedInvoicesCreatesEDocumentForEach

  • Posts two invoices without auto E-Document creation (DSP disabled during post)
  • Re-enables the extended E-Document service flow
  • Calls DocumentSendingProfile.Send() with both invoices in the filtered RecordRef
  • Asserts EDocument.Count() = 2 -- one E-Document per invoice

Test 2 - Single-document regression guard:
SendSinglePostedInvoiceCreatesOneEDocument

  • Same setup pattern but with a single invoice
  • Asserts EDocument.Count() = 1

Both tests follow the pattern from EDocEmailTests (bind EDocImplState, use LibraryEDoc.SetupStandardSalesScenario, disable DSP before posting, re-enable before sending). Local verification was also done on a BC 28 W1 Docker instance: Invoke-NAVCodeunit on the equivalent test codeunit completed with no failures in approximately 5 seconds.

Groenbech96
Groenbech96 previously approved these changes Apr 22, 2026
darjoo
darjoo previously approved these changes Apr 22, 2026
…irective

AL0792: The using directive was flagged as unused by the BC29 compiler.
Assert is resolved without it in this compilation context.
Copilot AI review requested due to automatic review settings April 22, 2026 23:27
@jeffreybulanadi jeffreybulanadi dismissed stale reviews from darjoo and Groenbech96 via 707bb4a April 22, 2026 23:27
@jeffreybulanadi jeffreybulanadi force-pushed the fix/5650-edocument-onaftersend-loop-all-records branch from 707bb4a to 88ceae2 Compare April 22, 2026 23:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes E-Document creation when sending multiple posted documents via a Document Sending Profile by iterating over all records in the RecordVariant set, and adds regression tests to validate multi-record vs single-record sending.

Changes:

  • Iterate through the full RecordVariant set in OnAfterSend and OnAfterSendToEMail subscribers to create an E-Document per selected posted document.
  • Add integration tests that verify E-Documents are created for both multi-invoice and single-invoice send scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Apps/W1/EDocument/App/src/Processing/EDocumentSubscribers.Codeunit.al Updates event subscribers to loop over all records in the passed RecordVariant before checking/creating E-Documents.
src/Apps/W1/EDocument/Test/src/Processing/EDocSendSubscriberTest.Codeunit.al Adds new integration tests covering multi-record and single-record send behavior for posted sales invoices.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Apps/W1/EDocument/App/src/Processing/EDocumentSubscribers.Codeunit.al Outdated
Comment thread src/Apps/W1/EDocument/App/src/Processing/EDocumentSubscribers.Codeunit.al Outdated
… RecordRef variants

Replace RecordRef.GetTable(RecordVariant) with the TypeHelper pattern already
used in EDocumentProcessing.Codeunit.al. GetTable only accepts a Record variant
and throws a runtime error when the caller passes a RecordRef variant.

TypeHelper.CopyRecVariantToRecRef handles both IsRecord() and IsRecordRef() cases,
preventing regressions when downstream callers pass a RecordRef. Add a guard
clause that exits early if the variant is neither type, consistent with the
existing pattern in EDocumentProcessing.GetTypeFromSourceDocument.
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 Approved The issue is approved From Fork Pull request is coming from a fork Linked Issue is linked to a Azure Boards work item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: e-Documents: EventSubscriber OnAfterSend does not loop the RecordVariant variable

4 participants