From a89e05a4c1bd84015c75c17b14fc2c3fff7fbb6a Mon Sep 17 00:00:00 2001 From: Fer Date: Thu, 26 Nov 2026 19:47:22 +0100 Subject: [PATCH 1/2] workflow-library-registration --- evaluation/review-fixtures.json | 10 ++++-- ...-libraries-through-extension-points.bad.al | 33 +++++++++++++++++++ ...libraries-through-extension-points.good.al | 31 +++++++++++++++++ ...flow-libraries-through-extension-points.md | 32 ++++++++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al create mode 100644 microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al create mode 100644 microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md diff --git a/evaluation/review-fixtures.json b/evaluation/review-fixtures.json index cfcf6be8..5e198f6a 100644 --- a/evaluation/review-fixtures.json +++ b/evaluation/review-fixtures.json @@ -79,8 +79,14 @@ "database-trigger-setup-flags-may-only-be-set-to-true", "isolated-events-are-isolated-only-outside-a-write-transaction", "isolated-event-rollback-does-not-undo-var-parameters", - "avoid-raising-events-inside-try-functions" - ] + "avoid-raising-events-inside-try-functions", + "register-workflow-libraries-through-extension-points" + ], + "articleOverrides": { + "register-workflow-libraries-through-extension-points": { + "context": "Target Business Central 29, worldwide. The shown file contains all code that registers or recreates this extension's custom workflow library entries. An enabled workflow uses the custom purchase approval event and the standard Create Approval Requests response." + } + } }, "finance": { "articles": [ diff --git a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al new file mode 100644 index 00000000..d3e1c069 --- /dev/null +++ b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al @@ -0,0 +1,33 @@ +using Microsoft.Purchases.Document; +using System.Automation; + +codeunit 50181 "Purchase Workflow Install" +{ + Subtype = Install; + + trigger OnInstallAppPerCompany() + var + WorkflowEventHandling: Codeunit "Workflow Event Handling"; + begin + WorkflowEventHandling.AddEventToLibrary(PurchaseApprovalEventCodeLbl, Database::"Purchase Header", PurchaseApprovalEventDescription, 0, false); + end; + + var + PurchaseApprovalEventCodeLbl: Label 'SAMPLEPURCHASEAPPROVALREQUEST', Locked = true; + PurchaseApprovalEventDescription: Label 'A purchase approval is requested.'; +} + +codeunit 50182 "Purchase Workflow Registration" +{ + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Response Handling", 'OnAddWorkflowResponsePredecessorsToLibrary', '', false, false)] + local procedure RegisterPurchaseResponsePredecessor(ResponseFunctionName: Code[128]) + var + WorkflowResponseHandling: Codeunit "Workflow Response Handling"; + begin + if ResponseFunctionName = WorkflowResponseHandling.CreateApprovalRequestsCode() then + WorkflowResponseHandling.AddResponsePredecessor(ResponseFunctionName, PurchaseApprovalEventCodeLbl); + end; + + var + PurchaseApprovalEventCodeLbl: Label 'SAMPLEPURCHASEAPPROVALREQUEST', Locked = true; +} diff --git a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al new file mode 100644 index 00000000..690aeeec --- /dev/null +++ b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al @@ -0,0 +1,31 @@ +using Microsoft.Purchases.Document; +using System.Automation; + +codeunit 50180 "Purchase Workflow Registration" +{ + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Event Handling", 'OnAddWorkflowEventsToLibrary', '', false, false)] + local procedure RegisterPurchaseWorkflowEvent() + var + WorkflowEventHandling: Codeunit "Workflow Event Handling"; + begin + WorkflowEventHandling.AddEventToLibrary(PurchaseApprovalEventCode(), Database::"Purchase Header", PurchaseApprovalEventDescription, 0, false); + end; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Response Handling", 'OnAddWorkflowResponsePredecessorsToLibrary', '', false, false)] + local procedure RegisterPurchaseResponsePredecessor(ResponseFunctionName: Code[128]) + var + WorkflowResponseHandling: Codeunit "Workflow Response Handling"; + begin + if ResponseFunctionName = WorkflowResponseHandling.CreateApprovalRequestsCode() then + WorkflowResponseHandling.AddResponsePredecessor(ResponseFunctionName, PurchaseApprovalEventCode()); + end; + + local procedure PurchaseApprovalEventCode(): Code[128] + begin + exit(PurchaseApprovalEventCodeLbl); + end; + + var + PurchaseApprovalEventCodeLbl: Label 'SAMPLEPURCHASEAPPROVALREQUEST', Locked = true; + PurchaseApprovalEventDescription: Label 'A purchase approval is requested.'; +} diff --git a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md new file mode 100644 index 00000000..0ce396e9 --- /dev/null +++ b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md @@ -0,0 +1,32 @@ +--- +bc-version: [all] +domain: events +keywords: [workflow, workflow-event, workflow-response, workflow-library, workflow-registration, upgrade, subscriber, onaddworkfloweventstolibrary] +technologies: [al] +countries: [w1] +application-area: [all] +--- + +# Register custom workflow library entries through their extension points + +## Description + +Business Central rebuilds the workflow event, response, and table relation libraries during an upgrade. Custom entries added only during installation can initially appear in the workflow editor but disappear when those libraries are recreated. The compiler cannot tell that the registration will be lost. + +## Best Practice + +Register each custom workflow event, response, or table relation through the corresponding workflow library extension point, so the framework can recreate it when the library is rebuilt. For example, subscribe to `OnAddWorkflowEventsToLibrary` in `Workflow Event Handling` and call `AddEventToLibrary` for the custom event. Register only the entries the extension adds; an existing standard response does not need a new custom response. + +See sample: [`register-workflow-libraries-through-extension-points.good.al`](register-workflow-libraries-through-extension-points.good.al). + +## Anti Pattern + +Adding a custom workflow library entry once from an install trigger without registering that same entry through the appropriate library extension point. The entry may work until an upgrade recreates the library, after which it is missing. Check the extension's other codeunits for a registration subscriber before reporting a missing registration; the subscriber need not be in the same file as the install trigger or the workflow handler. + +Do not flag a custom workflow event merely for being custom, or require a custom response when the extension uses an existing standard response. + +See sample: [`register-workflow-libraries-through-extension-points.bad.al`](register-workflow-libraries-through-extension-points.bad.al). + +## References + +- [Walkthrough: Implementing New Workflow Events and Responses](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/devenv-walkthrough-workflow-events-responses) From 15c2172cc25591b6d85216bccc9a7d217f9b7563 Mon Sep 17 00:00:00 2001 From: Fer Date: Sun, 29 Nov 2026 10:24:38 +0100 Subject: [PATCH 2/2] AddResponseToLibrary fix --- evaluation/review-fixtures.json | 2 +- ...ster-workflow-libraries-through-extension-points.bad.al | 7 +++---- ...ter-workflow-libraries-through-extension-points.good.al | 7 +++---- ...register-workflow-libraries-through-extension-points.md | 6 ++++++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/evaluation/review-fixtures.json b/evaluation/review-fixtures.json index 5e198f6a..1686e502 100644 --- a/evaluation/review-fixtures.json +++ b/evaluation/review-fixtures.json @@ -84,7 +84,7 @@ ], "articleOverrides": { "register-workflow-libraries-through-extension-points": { - "context": "Target Business Central 29, worldwide. The shown file contains all code that registers or recreates this extension's custom workflow library entries. An enabled workflow uses the custom purchase approval event and the standard Create Approval Requests response." + "context": "Target Business Central 28, worldwide. The shown file contains all code that registers or recreates this extension's custom workflow library entries. An enabled workflow uses the custom purchase approval event and the standard Create Approval Requests response." } } }, diff --git a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al index d3e1c069..d57723f9 100644 --- a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al +++ b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.bad.al @@ -19,13 +19,12 @@ codeunit 50181 "Purchase Workflow Install" codeunit 50182 "Purchase Workflow Registration" { - [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Response Handling", 'OnAddWorkflowResponsePredecessorsToLibrary', '', false, false)] - local procedure RegisterPurchaseResponsePredecessor(ResponseFunctionName: Code[128]) + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Response Handling", 'OnAddWorkflowResponsesToLibrary', '', false, false)] + local procedure RegisterPurchaseResponsePredecessor() var WorkflowResponseHandling: Codeunit "Workflow Response Handling"; begin - if ResponseFunctionName = WorkflowResponseHandling.CreateApprovalRequestsCode() then - WorkflowResponseHandling.AddResponsePredecessor(ResponseFunctionName, PurchaseApprovalEventCodeLbl); + WorkflowResponseHandling.AddResponsePredecessor(WorkflowResponseHandling.CreateApprovalRequestsCode(), PurchaseApprovalEventCodeLbl); end; var diff --git a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al index 690aeeec..c762fa8f 100644 --- a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al +++ b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.good.al @@ -11,13 +11,12 @@ codeunit 50180 "Purchase Workflow Registration" WorkflowEventHandling.AddEventToLibrary(PurchaseApprovalEventCode(), Database::"Purchase Header", PurchaseApprovalEventDescription, 0, false); end; - [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Response Handling", 'OnAddWorkflowResponsePredecessorsToLibrary', '', false, false)] - local procedure RegisterPurchaseResponsePredecessor(ResponseFunctionName: Code[128]) + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Workflow Response Handling", 'OnAddWorkflowResponsesToLibrary', '', false, false)] + local procedure RegisterPurchaseResponsePredecessor() var WorkflowResponseHandling: Codeunit "Workflow Response Handling"; begin - if ResponseFunctionName = WorkflowResponseHandling.CreateApprovalRequestsCode() then - WorkflowResponseHandling.AddResponsePredecessor(ResponseFunctionName, PurchaseApprovalEventCode()); + WorkflowResponseHandling.AddResponsePredecessor(WorkflowResponseHandling.CreateApprovalRequestsCode(), PurchaseApprovalEventCode()); end; local procedure PurchaseApprovalEventCode(): Code[128] diff --git a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md index 0ce396e9..6f56ecd3 100644 --- a/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md +++ b/microsoft/knowledge/events/register-workflow-libraries-through-extension-points.md @@ -17,6 +17,10 @@ Business Central rebuilds the workflow event, response, and table relation libra Register each custom workflow event, response, or table relation through the corresponding workflow library extension point, so the framework can recreate it when the library is rebuilt. For example, subscribe to `OnAddWorkflowEventsToLibrary` in `Workflow Event Handling` and call `AddEventToLibrary` for the custom event. Register only the entries the extension adds; an existing standard response does not need a new custom response. +When pairing a custom event with an existing standard response, register the combination with `AddResponsePredecessor` from `OnAddWorkflowResponsesToLibrary`. In BC 28, `AddResponseToLibrary` exits if the response already exists, so its `OnAddWorkflowResponsePredecessorsToLibrary` callback may not run for that response in an existing company. The sample registers the combination independently of that callback. + +The sample uses namespaces from BC 23 and later; remove its `using` directives when adapting it to earlier releases. It illustrates library registration, not the code that raises the workflow event or configures an approval workflow. + See sample: [`register-workflow-libraries-through-extension-points.good.al`](register-workflow-libraries-through-extension-points.good.al). ## Anti Pattern @@ -30,3 +34,5 @@ See sample: [`register-workflow-libraries-through-extension-points.bad.al`](regi ## References - [Walkthrough: Implementing New Workflow Events and Responses](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/devenv-walkthrough-workflow-events-responses) +- [Workflow Response Handling codeunit](https://learn.microsoft.com/dynamics365/business-central/application/base-application/codeunit/system.automation.workflow-response-handling) +- [Namespaces in AL](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/devenv-namespaces-overview)