feat: expose FindEmailSourceEntities and GetPrimarySourceEntity from EmailImpl and Email codeunits#7824
Open
jeffreybulanadi wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…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.
Contributor
There was a problem hiding this comment.
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
FindEmailSourceEntitiesandGetPrimarySourceEntitytoEmail Impl(8900) and exposed them publicly viaEmail(8901). - Refactored
Email Editor(8906) to delegate toEmail Implinstead 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, avar ... beginblock follows without aprocedure ...()header (andGetSentEmailsForRecordByVariantis 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.
- 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.
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 #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:
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):
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: