bcquality/microsoft/knowledge/events/prefer-this-over-includesender-in-codeunit-events.md
Jesper Schulz-Wedde 2b5550c346
Some checks failed
Validate knowledge index / validate-index (push) Has been cancelled
Validate AL review fixtures / validate-review-fixtures (push) Has been cancelled
Validate frontmatter and structure / validate (push) Has been cancelled
Improve partner onboarding and documentation navigation (#174)
Lead with a complete plugin quick start and add task-oriented usage, troubleshooting, customization, and contribution guides. Preserve the broader plugin framing, correct conflicting contract guidance, support Agents folder reviews, and align repository validation. Convert existing sample references to clickable links without changing knowledge rules.

Co-authored-by: Jesper Schulz-Wedde <jesper.schulzwedde@microsoft.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
2026-09-09 17:31:03 +02:00

1.6 KiB

bc-version domain keywords technologies countries application-area
25..
events
this-keyword
includesender
sender
codeunit
self-reference
integration-event
type-safety
al
w1
all

Prefer this over IncludeSender in new codeunit events

Description

When designing a new publisher, setting IncludeSender to true on [IntegrationEvent] or [BusinessEvent] gives subscribers the publishing object as an implicit sender parameter. From Business Central 2024 release wave 2, a codeunit can instead pass itself explicitly with the this keyword as a normal, strongly-typed Sender parameter. Explicit passing makes the sender visible and typed in the signature. This is new-event design guidance only: never change IncludeSender on an event that has already shipped.

Best Practice

For a new event, declare the publisher [IntegrationEvent(false, false)] with an explicit Sender: Codeunit "…" parameter and raise it with this, for example OnBeforeProcessOrder(OrderNo, this);. Subscribers then receive a typed sender they can call directly.

See sample: prefer-this-over-includesender-in-codeunit-events.good.al.

Anti Pattern

Designing a new codeunit event with [IntegrationEvent(true, …)] solely to hand subscribers the publisher instance, where this could be passed explicitly as a typed parameter. Do not apply this rule by mutating a shipped event's attribute flags.

See sample: prefer-this-over-includesender-in-codeunit-events.bad.al.