# Bc Attachments Generator

> Implements standard document attachments, links, and notes on custom Business Central tables. Creates setup fields for visibility control, extends Attachment Document Type enum, generates event subscribers (OnAfterGetRecRefFail, OnAfterOpenForRecRef, OnAfterInitFieldsFromRecRef) in attachment management codeunit, adds table lifecycle triggers for orphan prevention on delete/rename, and extends ca…

- **Type:** Skill
- **Install:** `agentstack add skill-fernandoartalf-al-copilot-skills-collection-bc-attachments-generator`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [fernandoartalf](https://agentstack.voostack.com/s/fernandoartalf)
- **Installs:** 0
- **Category:** [Developer Tools](https://agentstack.voostack.com/c/developer-tools)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [fernandoartalf](https://github.com/fernandoartalf)
- **Source:** https://github.com/fernandoartalf/AL-Copilot-Skills-Collection/tree/main/skills/bc-attachments-generator
- **Website:** https://alcopilotskills.com/

## Install

```sh
agentstack add skill-fernandoartalf-al-copilot-skills-collection-bc-attachments-generator
```

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

## About

# BC Attachments, Links & Notes Generator

Automates BC standard document attachment functionality on custom tables.

## Overview

Generates complete attachment, link, and notes implementation for custom BC tables following Microsoft patterns. Creates 3 event subscribers, lifecycle triggers, modern factbox configuration, and setup controls.

**Complete code templates available in**: [references/code-templates.md](references/code-templates.md)

## Prerequisites

- AL workspace with custom table
- Setup table (or will create one)
- Card and List pages for entity

## Workflow

### Step 1: Gather Information

Collect from user:
- Entity name (e.g., "Statistical Account")
- Table name and ID
- Prefix (e.g., "BCS")
- Primary key field (usually "No.")
- Setup table name
- Card and list page names

### Step 2: Update Setup Table

Add 3 boolean fields to setup table:
- `"Enable Attachments"` (InitValue = true)
- `"Enable Links"` (InitValue = true)
- `"Enable Notes"` (InitValue = true)

Location: `src/Table/[Prefix][SetupName].Table.al`

See [code-templates.md](references/code-templates.md#setup-table-fields) for complete code.

### Step 3: Update Setup Page

Add 3 fields to setup page layout in General group.

Location: `src/Page/[Prefix][SetupName].Page.al`

See [code-templates.md](references/code-templates.md#setup-page-fields) for complete code.

### Step 4: Create Enum Extension

Extend `"Attachment Document Type"` enum with custom value.

Location: `src/Enum Extension/[Prefix]AttachmentDocType.EnumExt.al`

```al
enumextension [ID] "[Prefix] Attachment Doc Type" extends "Attachment Document Type"
{
  value([ID]; [Prefix][Entity])  // Remove spaces: "Statistical Account" → "BCSStatisticalAccount"
  {
    Caption = '[Entity]';
  }
}
```

### Step 5: Create Attachment Management Codeunit

Create codeunit with 3 event subscribers + 5 procedures.

Location: `src/Codeunit/[Prefix]AttachmentManagement.Codeunit.al`

**Event Subscribers:**
1. `OnAfterGetRecRefFail` - Handle record reference resolution for attachment factbox (Page::"Doc. Attachment List Factbox")
2. `OnAfterOpenForRecRef` - Filter attachments when opening details (Page::"Document Attachment Details")
3. `OnAfterInitFieldsFromRecRef` - Initialize attachment from record (Table::"Document Attachment")

**Procedures:**
1. `EntityEnabledAttachments(TableId)` - Check if attachments enabled
2. `EntityEnabledLinks(TableId)` - Check if links enabled
3. `EntityEnabledNotes(TableId)` - Check if notes enabled
4. `DeleteRelatedDocumentAttachments(No)` - Delete attachments on record delete
5. `CopyRelatedDocumentAttachments(OldNo, NewNo)` - Copy attachments on rename

See [code-templates.md](references/code-templates.md#complete-attachment-management-codeunit) for complete code.

### Step 6: Add Table Lifecycle Triggers

Extend table with:
- `OnBeforeRename()` - Copy attachments to new No., delete old ones
- `OnBeforeDelete()` - Delete related attachments to prevent orphans

Location: Update existing table extension

See [code-templates.md](references/code-templates.md#table-extension-triggers) for complete code.

### Step 7: Extend Card Page

Add factboxes section with:
- Modern factbox: `"Doc. Attachment List Factbox"` (UpdatePropagation = Both)
- Modify Links factbox: `Control1900383207`
- Modify Notes factbox: `Control1905767507`

> **Note**: The legacy `"Document Attachment Factbox"` is fully deprecated and must NOT be included.

Add `OnOpenPage()` trigger to set visibility variables.

Location: `src/Page Extension/[Prefix][Entity]Card.PageExt.al`

**SubPageLink configuration:**
```al
SubPageLink = "Table ID" = const(Database::"[Base Table]"),
              "No." = field("No.");
```

See [code-templates.md](references/code-templates.md#page-extension-card-page-factboxes) for complete code.

**Control ID Note**: If modify fails (e.g., "Control not found"), inspect base page to find actual IDs. They may vary between BC versions.

### Step 8: Extend List Page

Repeat Step 7 for list page.

Location: `src/Page Extension/[Prefix][Entity]List.PageExt.al`

### Step 9: Update Permissions

Add to permission set:
```al
codeunit "[Prefix] Attachment Management" = X;
```

### Step 10: Test

**Critical tests:**
1. Upload attachment → verify appears in factbox
2. Delete record → verify attachments deleted
3. Rename record → verify attachments follow (copied to new No.)
4. Navigate between records → verify factbox refreshes
5. Toggle setup fields → verify factboxes hide/show

## Troubleshooting

### Factboxes Not Appearing
- Check setup page: ensure "Enable Attachments/Links/Notes" checked
- Verify `OnOpenPage()` trigger runs and sets variables
- Confirm `DATABASE::"[Table]"` matches exactly (case-sensitive)

### Attachments Not Filtering
- Verify enum extension exists and builds first
- Check `SubPageLink` includes correct `"Table ID"` and `"No."` field
- Confirm `OnAfterInitFieldsFromRecRef` sets `"Document Type"` correctly

### Orphan Attachments After Delete/Rename
- Missing `OnBeforeDelete` trigger → Add and call `DeleteRelatedDocumentAttachments()`
- Missing `OnBeforeRename` trigger → Add `RenameAttachments()` procedure

### Links/Notes Factbox Visibility Not Working
- Wrong control IDs → Inspect base page to find actual IDs
- Common IDs: `1900383207` (Links), `1905767507` (Notes)
- Base page may not have these factboxes

### Factbox Not Refreshing on Navigation
- Missing `UpdatePropagation = Both` on modern factbox
- Add to `"Doc. Attachment List Factbox"` part

## Multi-Entity Pattern

When adding attachments to multiple tables in one extension, use case statements in all event subscribers and procedures.

See [code-templates.md](references/code-templates.md#multi-entity-support-pattern) for complete example.

## Storage Best Practice

BC has database storage limits. Recommend:
- Small files in BC attachments (< 1 MB)
- Large files in SharePoint/OneDrive
- Use Links factbox for SharePoint URLs

## Quick Reference

**Required Objects:**
- Setup fields (3 booleans)
- Enum extension
- Attachment management codeunit (3 event subscribers, 5 procedures)
- Table triggers (OnBeforeDelete, OnBeforeRename)
- Page factboxes (card and list)

**Event Subscribers:**
- `OnBeforeDrillDown` (obsolete factbox)
- `OnAfterOpenForRecRef` (details page)
- `OnAfterInitFieldsFromRecRef` (initialization)

**Factbox Control IDs:**
- Links: `Control1900383207`
- Notes: `Control1905767507`
- May vary by BC version

**Real-world example:** [BC Scout Statistical Accounts](https://github.com/fernandoartalf/BC-Scout-Path)

3. Rename record → verify attachments follow (copied to new No.)
4. Navigate between records → verify factbox refreshes
5. Toggle setup fields → verify factboxes hide/show

## Troubleshooting

### Factboxes Not Appearing
- Check setup page: ensure "Enable Attachments/Links/Notes" checked
- Verify `OnOpenPage()` trigger runs and sets variables
- Confirm `DATABASE::"[Table]"` matches exactly (case-sensitive)

### Attachments Not Filtering
- Verify enum extension exists and builds first
- Check `SubPageLink` includes correct `"Table ID"` and `"No."` field
- Confirm `OnAfterInitFieldsFromRecRef` sets `"Document Type"` correctly

### Orphan Attachments After Delete/Rename
- Missing `OnBeforeDelete` trigger → Add and call `DeleteRelatedDocumentAttachments()`
- Missing `OnBeforeRename` trigger → Add `RenameAttachments()` procedure

### Links/Notes Factbox Visibility Not Working
- Wrong control IDs → Inspect base page to find actual IDs
- Common IDs: `1900383207` (Links), `1905767507` (Notes)
- Base page may not have these factboxes

### Factbox Not Refreshing on Navigation
- Missing `UpdatePropagation = Both` on modern factbox
- Add to `"Doc. Attachment List Factbox"` part

## Multi-Entity Pattern

When adding attachments to multiple tables in one extension, use case statements in all event subscribers and procedures.

See [code-templates.md](references/code-templates.md#multi-entity-support-pattern) for complete example.

## Storage Best Practice

BC has database storage limits. Recommend:
- Small files in BC attachments (< 1 MB)
- Large files in SharePoint/OneDrive
- Use Links factbox for SharePoint URLs

## Quick Reference

**Required Objects:**
- Setup fields (3 booleans)
- Enum extension
- Attachment management codeunit (3 event subscribers, 5 procedures)
- Table triggers (OnBeforeDelete, OnBeforeRename)
- Page factboxes (card and list)

**Event Subscribers:**
- `OnBeforeDrillDown` (obsolete factbox)
- `OnAfterOpenForRecRef` (details page)
- `OnAfterInitFieldsFromRecRef` (initialization)

**Factbox Control IDs:**
- Links: `Control1900383207`
- Notes: `Control1905767507`
- May vary by BC version

**Real-world example:** [BC Scout Statistical Accounts](https://github.com/fernandoartalf/BC-Scout-Path)

## Source & license

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

- **Author:** [fernandoartalf](https://github.com/fernandoartalf)
- **Source:** [fernandoartalf/AL-Copilot-Skills-Collection](https://github.com/fernandoartalf/AL-Copilot-Skills-Collection)
- **License:** MIT
- **Homepage:** https://alcopilotskills.com/

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-fernandoartalf-al-copilot-skills-collection-bc-attachments-generator
- Seller: https://agentstack.voostack.com/s/fernandoartalf
- 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%.
