# Binding Event Subscribers

> Turns event subscribers on and off dynamically using BindSubscription() and UnbindSubscription() with EventSubscriberInstance = Manual for conditional event handling. Use when disabling subscribers during batch jobs, enabling page-scoped validation, implementing feature toggles via setup, turning off event handlers for performance, or controlling dynamic event execution at runtime.

- **Type:** Skill
- **Install:** `agentstack add skill-emgreppi-business-central-ai-skill-binding-event-subscribers`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [emgreppi](https://agentstack.voostack.com/s/emgreppi)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [emgreppi](https://github.com/emgreppi)
- **Source:** https://github.com/emgreppi/Business-Central-AI-Skill/tree/main/binding-event-subscribers

## Install

```sh
agentstack add skill-emgreppi-business-central-ai-skill-binding-event-subscribers
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Skill: AL Event Binding

## Validation Gates

1. **After Step 1**: Manual codeunit compiles, subscribers don't fire without binding
2. **After Step 2**: BindSubscription activates subscriber, events fire correctly
3. **Final**: UnbindSubscription deactivates, no events fire after unbind

**Note:** Default to `StaticAutomatic`. Use Manual only for conditional/performance scenarios. Binding is session-specific.

## Procedure

### Step 1: Create Manual Subscriber Codeunit

```al
codeunit  "  Subscribers"
{
    EventSubscriberInstance = Manual;

    [EventSubscriber(ObjectType::Table, Database::, 'OnAfterValidateEvent', '', false, false)]
    local procedure OnValidate(var Rec: Record )
    begin
        // Your validation logic here
        if StrLen(Rec.)  must be at least 3 characters');
    end;
}
```

### Step 2: Bind in Context (Page/Codeunit)

```al
pageextension  "  Card Ext" extends ""
{
    var
        EntitySubscribers: Codeunit "  Subscribers";

    trigger OnOpenPage()
    begin
        BindSubscription(EntitySubscribers);
    end;

    trigger OnClosePage()
    begin
        UnbindSubscription(EntitySubscribers);
    end;
}
```

## Common Patterns

### Conditional via Setup

```al
procedure ProcessDocument(var SourceRecord: Record )
var
    AdvancedValidation: Codeunit " Advanced Validation";
    Setup: Record " Setup";
begin
    if Setup.Get() and Setup."Enable Advanced Validation" then
        BindSubscription(AdvancedValidation);

    Codeunit.Run(Codeunit::, SourceRecord);
    // Auto-unbinds when procedure ends
end;
```

### Batch Job Optimization

UI subscribers bound only in pages, not in job queue codeunits:
- Page: `OnOpenPage` → `BindSubscription(UINotifications)`
- Job Queue: No binding → no UI overhead

## Important Notes

**Binding behavior:**
- Session-specific (Session A binding doesn't affect Session B)
- Auto-dissolves when codeunit instance goes out of scope
- Cannot call Bind/Unbind on `StaticAutomatic` codeunits (runtime error)

**Stale metadata:** Recompiling bound codeunit shows error "subscriber is stale" - only during development, not production.

**⚠️** Do NOT use Manual for critical business logic that must never be missed.

## External Documentation

- [EventSubscriberInstance Property](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-eventsubscriberinstance-property)
- [BindSubscription Method](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/session/session-bindsubscription-method)

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [emgreppi](https://github.com/emgreppi)
- **Source:** [emgreppi/Business-Central-AI-Skill](https://github.com/emgreppi/Business-Central-AI-Skill)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-emgreppi-business-central-ai-skill-binding-event-subscribers
- Seller: https://agentstack.voostack.com/s/emgreppi
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
