# Business Central AL Event Subscriber Code Generator

> Source: https://netray.co/tools/business-central-al-event-subscriber-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Builds an AL subscriber codeunit for any published event, with the correct attribute syntax and a procedure stub to fill in.

## Category

developer

## Erp

Microsoft Dynamics 365 Business Central

## Audience

- BC developer
- AL developer
- NAV developer

## Tool

**Kind:** template

**Output:** code

**Language:** text

**Fields:**

#### Subscriber codeunit ID

**Key:** obj_id

**Type:** number

**Default:** 50100

**Help:** An ID in your extension range from app.json

#### Subscriber codeunit name

**Key:** obj_name

**Type:** text

**Default:** NTR Sales Post Subscribers

#### Publisher object type

**Key:** pub_type

**Type:** select

**Options:**

- Codeunit
- Table
- Page
- Report
- Query
- XmlPort

**Default:** Codeunit

#### Publisher object name

**Key:** pub_name

**Type:** text

**Default:** Sales-Post

#### Event name

**Key:** event

**Type:** text

**Default:** OnBeforePostSalesDoc

#### Procedure name

**Key:** proc

**Type:** text

**Default:** CheckBeforePostSalesDoc

#### Event parameters

**Key:** params

**Type:** textarea

**Default:** var SalesHeader: Record "Sales Header"; CommitIsSuppressed: Boolean; PreviewMode: Boolean; var HideProgressWindow: Boolean; var IsHandled: Boolean

**Help:** Copy the parameter list from the publisher so signatures match exactly

#### Skip on missing license

**Key:** skip_lic

**Type:** select

**Options:**

- false
- true

**Default:** false

#### Skip on missing permission

**Key:** skip_perm

**Type:** select

**Options:**

- false
- true

**Default:** false

**Blocks:**

#### AL codeunit

**Body:** codeunit {{obj_id}} "{{obj_name}}"
{
    Access = Internal;

    [EventSubscriber(ObjectType::{{pub_type}}, {{pub_type}}::"{{pub_name}}", '{{event}}', '', {{skip_lic}}, {{skip_perm}})]
    local procedure {{proc}}({{params}})
    begin
        // Add your logic here. Keep it small and avoid user dialogs in posting events.
    end;
}

**Notes:** Publisher names with spaces or hyphens must be quoted, as shown. Check the event signature in the publisher source (the Base Application is public on GitHub) because parameter names, order and var keywords must match. The last two boolean arguments are SkipOnMissingLicense and SkipOnMissingPermission, confirm against current AL documentation for your runtime version.

## Content

**Intro:** Business Central customization is done with extensions that react to events instead of editing base code. This generator writes the subscriber codeunit and the EventSubscriber attribute so you do not have to remember the syntax.

**Sections:**

#### How to use the generator

**Body:** Fill in the object type and name of the publisher, the event name and the parameter list. Paste the parameter list from the publisher so the signature matches exactly. Copy the output into a new .al file in your extension, adjust the object ID to your range and add your logic inside begin and end. Build the extension with the AL Language extension for Visual Studio Code.

**Bullets:**

- Find events by searching the Base Application source or the Event Recorder page
- Keep the subscriber codeunit name unique with your prefix
- Use a separate procedure per event for readability

#### Anatomy of the attribute

**Body:** The EventSubscriber attribute takes the object type, the object, the event name, an element name used for field level events, and two Boolean values that control skipping when the license or permission is missing. For business events the element name is an empty string. For table events such as OnAfterValidateEvent, the element name is the field name. The procedure that follows must be local or global but not require parameters other than those published.

#### Good practice for subscribers

**Body:** Subscribers run inside the posting transaction, so an error rolls back the document. Do not open dialogs, send mail or call slow web services inside posting events. Respect the IsHandled pattern: if the publisher offers IsHandled, check it first and set it only when you fully take over the logic. Wrap new behavior in a setup flag so it can be turned off without redeployment.

#### Common mistakes

**Body:** Signature mismatches produce a compile error, so copy parameters exactly including var. Another mistake is subscribing to a broad table event like OnAfterInsertEvent and running heavy code. Developers also forget that events can be raised during preview posting, so check PreviewMode where relevant. Finally, never modify the Base Application, only extend it.

**Cta:** If your extension work involves reports or integrations around posting, we can help design them with human approval on any write-back.

**Faq:**

#### How do I find the event I need?

**Answer:** Use the Event Recorder page in Business Central to record the events raised while you perform an action, or search the public Base Application source on GitHub for the codeunit and look for IntegrationEvent and BusinessEvent attributes.

#### What happens if my parameter list is wrong?

**Answer:** The AL compiler gives an error because the subscriber signature must match the published event. Names can differ in some cases, but the types, order and var modifiers must match, so copy from the publisher.

#### Can a subscriber change the outcome of posting?

**Answer:** Yes. Parameters passed by var can be changed, and an Error call stops the transaction. Use this carefully and document the behavior, because other extensions may subscribe to the same event.

#### Is this the same as C/AL triggers in NAV?

**Answer:** No. In classic NAV you edited code in base objects. In Business Central extensions you subscribe to published events and never change the base, which is what makes upgrades manageable.

## Search terms

business central event subscriber, al eventsubscriber example, business central onbeforepostsalesdoc, al subscriber codeunit, business central extension events, al language event subscriber syntax, navision to business central events

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
