Event subscriber source generator
The Event subscriber source generator window writes the AL code of an event subscriber for you:
the EventSubscriber attribute and an empty local procedure with the right parameters. Choose the
options, copy the code and paste it into a codeunit of your own. The code changes as you change the options.
Opening it
Select Generate Event Subscriber Code… in the context menu of one of these, in the Object view:
- An event: a procedure with a business, external business, integration or internal event attribute.
- A trigger event of a table or a page, such as
OnAfterInsertEvent. - A field of a table, including a system field; also in the field's menu in the Outline panel.
- A field or an action in the layout of a page.
On a table or a page, the toolbar of the Objects page has a button with the tooltip “Generate trigger event subscriber code.”, which opens it for the object's trigger events. The window stays open while you go on working in the workspace.
OnAfterValidateEvent.| # | Name | Description |
|---|---|---|
| 1 | Event | “Event publisher to subscribe to”. See Event and element. |
| 2 | Element | “Element to observe”: the field or action the event is for. |
| 3 | Options | “Event subscriber options”. Expanded by default. See Options. |
| 4 | Event prefix | The start of the subscriber procedure's name. |
| 5 | Skip on missing license | Sets the attribute's SkipOnMissingLicense argument. |
| 6 | Skip on missing permission | Sets the attribute's SkipOnMissingPermission argument. |
| 7 | Rec requirement | “How the subscriber treats temporary records”. See Rec requirement. |
| 8 | Best Practices | Opens the AL Guidelines best practice on the IsTemporary table safeguard in your browser. |
| 9 | Add link to temporary record safeguard best practices | Adds a comment with the link to that best practice above the safeguard. |
| 10 | Copy to clipboard | Copies the code. See Event subscriber source. |
| 11 | Event subscriber source | The generated code. It is read-only: select and copy any part of it. |
Event and element
For an event, Event shows the event's name. For a trigger event it is a list of the trigger events that fit what you opened the window on, with the one you chose selected:
| Opened on | Events in the list | Element |
|---|---|---|
| A table, or one of its trigger events | OnBeforeInsertEvent, OnBeforeModifyEvent, OnBeforeDeleteEvent, OnBeforeRenameEvent, OnAfterInsertEvent, OnAfterModifyEvent, OnAfterDeleteEvent, OnAfterRenameEvent |
Not shown |
| A table field | OnBeforeValidateEvent, OnAfterValidateEvent |
The field |
| A page, or one of its trigger events | OnOpenPageEvent, OnClosePageEvent, OnAfterGetCurrRecordEvent, OnAfterGetRecordEvent, OnNewRecordEvent, OnInsertRecordEvent, OnModifyRecordEvent, OnDeleteRecordEvent, OnQueryClosePageEvent |
Not shown |
| A field in a page layout | OnBeforeValidateEvent, OnAfterValidateEvent |
The field |
| An action in a page layout | OnBeforeActionEvent, OnAfterActionEvent |
The action |
When the declaration comes from an extension, the subscriber names the object the extension extends.
Options
Only the options that apply to the event show. For an event with an include-sender argument, the window looks like this:
| # | Name | Description |
|---|---|---|
| 1 | Event | The event's name. |
| 2 | Include sender parameter | Adds the Sender parameter. |
| 3 | Event subscriber source | The subscriber, with the event's own parameters. |
| Option | Shown for | Default | Effect |
|---|---|---|---|
| Event prefix | All | Handle |
“Prefix added to the generated subscriber procedure name”. The procedure is named the prefix followed by
the event's name, such as HandleOnAfterValidate for OnAfterValidateEvent, and
is quoted when the name needs it.
|
| Skip on missing license | All | Off | On writes true as the attribute's SkipOnMissingLicense argument, so Business Central skips the subscriber when the license does not cover the codeunit it is in. |
| Skip on missing permission | All | Off | On writes true as the attribute's SkipOnMissingPermission argument, so Business Central skips the subscriber when the user has no permission to the codeunit it is in. |
| Include sender parameter | Business, integration and internal events whose attribute includes the sender | On | Adds a first parameter, Sender, of the type of the object that publishes the event. |
| Rec requirement | Table trigger events and table fields | Persistent |
“How the subscriber treats temporary records”. Persistent starts the procedure with
if Rec.IsTemporary then exit;, so the subscriber acts only on records in the database;
Temporary with if not Rec.IsTemporary then exit;, so it acts only on
temporary records; None adds no check. The Best Practices link
explains why.
|
| Add link to temporary record safeguard best practices | Table trigger events and table fields, while Rec requirement is not None | Off | Adds the comment // https://alguidelines.dev/docs/bestpractices/istemporary-table-safeguard/ above the check. |
Event subscriber source
The code under Event subscriber source follows the options. Select the copy button beside the
heading to copy all of it, indented by four spaces so it fits inside a codeunit; Prism confirms that it copied it.
For the OnAfterValidateEvent of the Name field of the Customer
table, with the default options, it reads:
[EventSubscriber(ObjectType::Table, Database::Customer, OnAfterValidateEvent, Name, false, false)]
local procedure HandleOnAfterValidate(var Rec: Record Customer)
begin
if Rec.IsTemporary then
exit;
// TODO
end;
- The attribute names the object type, the object, the event and the element, followed by the two skip arguments. An event without an element has
''as its element. - A table trigger event or field gets the parameter
var Recof the table's record type. - An event gets the event's own parameters, after
Senderwhen it is included. - The body holds the Rec requirement check, if any, and a
// TODOwhere your code goes.
Copy the default code without the window
On an event or a trigger event in the object view, Copy Default Event Subscriber Code to Clipboard copies the code this window shows with the default options, without opening it. Paste it into your codeunit.