Skip to content

feat: expose FindEmailSourceEntities and GetPrimarySourceEntity from EmailImpl and Email codeunits#7824

Open
jeffreybulanadi wants to merge 2 commits intomicrosoft:mainfrom
jeffreybulanadi:fix/4540-email-expose-source-entity-procedures
Open

feat: expose FindEmailSourceEntities and GetPrimarySourceEntity from EmailImpl and Email codeunits#7824
jeffreybulanadi wants to merge 2 commits intomicrosoft:mainfrom
jeffreybulanadi:fix/4540-email-expose-source-entity-procedures

Conversation

@jeffreybulanadi
Copy link
Copy Markdown

@jeffreybulanadi jeffreybulanadi commented Apr 23, 2026

Fixes #4540

Summary

The procedures GetPrimarySourceEntity and FindEmailSourceEntities were previously local to EmailEditor (codeunit 8906), making them inaccessible to scenarios that send emails programmatically without opening the editor (e.g. Word template emails sent via email flows, referenced in ALAppExtensions issues #29062 and #29061).

Changes

EmailImpl.Codeunit.al (codeunit 8900)

Added two internal procedures following the existing delegation pattern used by HasSourceRecord, AddRelation and RemoveRelation:

  • FindEmailSourceEntities(EmailMessageId: Guid; var Dict: Dictionary of [Integer, Text]): Boolean
  • GetPrimarySourceEntity(var PrimarySource: Integer; EmailMessageId: Guid; RelatedIds: List of [Integer]): Boolean

Email.Codeunit.al (codeunit 8901)

Exposed both procedures publicly with XML documentation, using EmailMessage: Codeunit parameters consistent with the existing public API (AddRelation, RemoveRelation, HasRelations):

  • FindEmailSourceEntities(EmailMessage: Codeunit "Email Message"; var Dict: Dictionary of [Integer, Text]): Boolean
  • GetPrimarySourceEntity(var PrimarySource: Integer; EmailMessage: Codeunit "Email Message"; RelatedIds: List of [Integer]): Boolean

EmailEditor.Codeunit.al (codeunit 8906)

Removed the two local procedure copies and refactored LoadWordTemplate and AttachFromWordTemplate to delegate to EmailImpl, keeping behaviour identical.

EmailTest.Codeunit.al

Added 4 automated tests:

  • FindEmailSourceEntitiesWithRelatedRecord - verifies source entities are returned correctly
  • FindEmailSourceEntitiesWithNoRelatedRecord - verifies false is returned when no entities exist
  • GetPrimarySourceEntityWithSingleSource - verifies single-source shortcut path
  • GetPrimarySourceEntityWithMultipleSources - verifies correct primary is identified among multiple relations

…EmailImpl and Email codeunits

Fixes microsoft#4540

The procedures GetPrimarySourceEntity and FindEmailSourceEntities were
previously local to EmailEditor (codeunit 8906), making them inaccessible
to scenarios that send emails programmatically without opening the editor
(e.g. Word template emails sent via email flows).

Changes:
- Move logic of both procedures to EmailImpl (codeunit 8900) as internal
  procedures, following the existing delegation pattern used by HasSourceRecord,
  AddRelation and RemoveRelation.
- Expose both procedures publicly through Email (codeunit 8901) using
  EmailMessage codeunit parameters, consistent with the existing public API.
- Refactor EmailEditor to delegate to EmailImpl instead of using local copies.
- Add automated tests covering: source found, no source, single source
  primary resolution, and multi-source primary resolution.
Copilot AI review requested due to automatic review settings April 23, 2026 00:38
@jeffreybulanadi jeffreybulanadi requested a review from a team as a code owner April 23, 2026 00:38
@github-actions github-actions Bot added AL: System Application From Fork Pull request is coming from a fork labels Apr 23, 2026
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

This PR makes the email “source entity” lookup logic available outside the Email Editor so that programmatic email-sending scenarios (e.g., Word template flows) can discover related entities and determine the primary source entity.

Changes:

  • Added FindEmailSourceEntities and GetPrimarySourceEntity to Email Impl (8900) and exposed them publicly via Email (8901).
  • Refactored Email Editor (8906) to delegate to Email Impl instead of keeping local copies.
  • Added new automated tests covering both procedures and key relation scenarios.

Reviewed changes

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

File Description
src/System Application/Test/Email/src/EmailTest.Codeunit.al Adds tests for FindEmailSourceEntities and GetPrimarySourceEntity behavior.
src/System Application/App/Email/src/Email/Outbox/EmailEditor.Codeunit.al Removes local helper procedures and delegates to Email Impl.
src/System Application/App/Email/src/Email/EmailImpl.Codeunit.al Implements the helper procedures that query Email Related Record.
src/System Application/App/Email/src/Email/Email.Codeunit.al Public API surface + XML docs delegating to Email Impl.
Comments suppressed due to low confidence (1)

src/System Application/Test/Email/src/EmailTest.Codeunit.al:1472

  • There appears to be a missing procedure declaration here: after GetPrimarySourceEntityWithMultipleSources() ends, a var ... begin block follows without a procedure ...() header (and GetSentEmailsForRecordByVariant is no longer present in the file). This will not compile and also drops the intended [Test] wrapper for the subsequent scenario. Reintroduce the procedure signature/attributes (or move this block back under the correct procedure).
    var
        SentEmail: Record "Sent Email";
        TempSentEmail: Record "Sent Email" temporary;
        TempEmailAccount: Record "Email Account";
        EmailMessage: Codeunit "Email Message";
        ConnectorMock: Codeunit "Connector Mock";
        Any: Codeunit Any;
        SystemId: Guid;
        TableId, NumberOfEmails, i : Integer;
        MessageIds: List of [Guid];
    begin
        // [Scenario] When successfully sending an email with source, GetSentEmailsForRecord return related Sent Emails. 

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

Comment thread src/System Application/App/Email/src/Email/EmailImpl.Codeunit.al
Comment thread src/System Application/App/Email/src/Email/EmailImpl.Codeunit.al Outdated
- FindEmailSourceEntities: track FindSet() result in a boolean variable
  and exit with it, avoiding the extra COUNT database roundtrip that
  the previous exit(EmailRelatedRecord.Count() > 0) caused.
- GetPrimarySourceEntity: replace SetFilter(Format(RelatedId)) with
  SetRange('Table Id', RelatedId) to eliminate locale-dependent integer
  formatting that could break filter matching on regional settings.
- EmailTest: restore missing [Test] + procedure declaration for
  GetSentEmailsForRecordByVariant that was accidentally omitted when
  inserting the new test procedures, causing a compile error.
@JesperSchulz JesperSchulz added the Integration GitHub request for Integration area label Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AL: System Application From Fork Pull request is coming from a fork Integration GitHub request for Integration area

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Email-Editor: procedure GetPrimarySourceEntity and FindEmailSourceEntities from Internal to global

3 participants