Reference manual

Objects page

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.

The window for a table field, with the code for its OnAfterValidateEvent.
#NameDescription
1Event“Event publisher to subscribe to”. See Event and element.
2Element“Element to observe”: the field or action the event is for.
3Options“Event subscriber options”. Expanded by default. See Options.
4Event prefixThe start of the subscriber procedure's name.
5Skip on missing licenseSets the attribute's SkipOnMissingLicense argument.
6Skip on missing permissionSets the attribute's SkipOnMissingPermission argument.
7Rec requirement“How the subscriber treats temporary records”. See Rec requirement.
8Best PracticesOpens the AL Guidelines best practice on the IsTemporary table safeguard in your browser.
9Add link to temporary record safeguard best practicesAdds a comment with the link to that best practice above the safeguard.
10Copy to clipboardCopies the code. See Event subscriber source.
11Event subscriber sourceThe 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 onEvents in the listElement
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:

The window for an integration event that includes its sender.
#NameDescription
1EventThe event's name.
2Include sender parameterAdds the Sender parameter.
3Event subscriber sourceThe subscriber, with the event's own parameters.
OptionShown forDefaultEffect
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 Rec of the table's record type.
  • An event gets the event's own parameters, after Sender when it is included.
  • The body holds the Rec requirement check, if any, and a // TODO where 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.