Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions evaluation/review-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 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."
}
}
},
"finance": {
"articles": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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", 'OnAddWorkflowResponsesToLibrary', '', false, false)]
local procedure RegisterPurchaseResponsePredecessor()
var
WorkflowResponseHandling: Codeunit "Workflow Response Handling";
begin
WorkflowResponseHandling.AddResponsePredecessor(WorkflowResponseHandling.CreateApprovalRequestsCode(), PurchaseApprovalEventCodeLbl);
end;

var
PurchaseApprovalEventCodeLbl: Label 'SAMPLEPURCHASEAPPROVALREQUEST', Locked = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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", 'OnAddWorkflowResponsesToLibrary', '', false, false)]
local procedure RegisterPurchaseResponsePredecessor()
var
WorkflowResponseHandling: Codeunit "Workflow Response Handling";
begin
WorkflowResponseHandling.AddResponsePredecessor(WorkflowResponseHandling.CreateApprovalRequestsCode(), PurchaseApprovalEventCode());
end;

local procedure PurchaseApprovalEventCode(): Code[128]
begin
exit(PurchaseApprovalEventCodeLbl);
end;

var
PurchaseApprovalEventCodeLbl: Label 'SAMPLEPURCHASEAPPROVALREQUEST', Locked = true;
PurchaseApprovalEventDescription: Label 'A purchase approval is requested.';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
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.

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

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)
- [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)