EventSubscriber Attribute

Version: Available or changed with runtime version 1.0.

Specifies the event to which the method subscribes.

Applies To

  • Method

Note

The EventSubscriber attribute can only be set inside codeunits.

Syntax

[EventSubscriber(ObjectType: ObjectType, ObjectId: Integer, EventName: Text, ElementName: Text, SkipOnMissingLicense: Boolean, SkipOnMissingPermission: Boolean)]

Arguments

ObjectType
 Type: ObjectType
Specifies the type of object that publishes the event to subscribe to.

ObjectId
 Type: Integer
Specifies the ID of the object that that publishes the event to subscribe to. You can specify the object by its ID (integer) or by its name using the syntax <ObjectType>::<ObjectName>, such as Codeunit::MyEventPublisher. Using the name is the recommended way.

EventName
 Type: Text
Specifies the name of method that publishes the event in the object that is specified by the ObjectId parameter.

ElementName
 Type: Text
Specifies the table field that the trigger event pertains to. This argument only requires a value for database trigger events, that is, when the ObjectType is set to table and the EventName argument is a validate trigger event, such as OnAfterValidateEvent.

SkipOnMissingLicense
 Type: Boolean
Specifies what happens to the event subscriber method call when the Dynamics 365 Business Central license of the user account that is running the current session does not include the codeunit that contains the subscriber method.

SkipOnMissingPermission
 Type: Boolean
Specifies what happens to the subscriber method call when the user account that is running the current session does not have permission to the codeunit that contains the event subscriber method.

Remarks

Tip

Use the Shift+Alt+E** shortcut in the AL code editor to invoke the list of all business, integration, and internal events. Select the one you want to subscribe to and an event subscriber for the event will be inserted.

The value of the ObjectType argument will depend on the type of event: business, integration, and trigger.

  • Business and integration events are published by event publisher methods in any of the valid object types. To subscribe to a business or integration type event, you specify the object that contains the event publisher method that defines the event.

  • Trigger events are system events that are automatically declared and published in table and page object. To subscribe to a trigger event, you specify the Table or Page, depending on where the trigger published from.

Important

If the ObjectType is set to Table, you speficy ObjectId by its name using the syntax Database::<ObjectName>, instead of Table::<ObjectName>.

For the SkipOnMissingLicense and SkipOnMissingPermission arguments, True will ignore the method call, and the code execution will continue to the next subscriber; false will throw an error and the code execution stops. false is the default.

Example 1

This example publishes an integration type event by using the OnAddressLineChanged method. The method takes a single text data type parameter. The IncludeSender and GlobalVarAccess arguments are set to false.

codeunit 50105 MyEventPublisher
{
    [IntegrationEvent(false, false)]
    procedure OnAddressLineChanged(line : Text[100]);
    begin
    end;
}

codeunit 50106 MyEventSubscriber
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::MyEventPublisher, 'OnAddressLineChanged', '', true, true)]
    local procedure MyProcedure(line: Text[100])
    begin
    end;
}

Example 2

In this example, the OnAfterValidateLocationCodePurchase method is subscribed to an OnAfterValidateEvent (Table) Trigger Event. Therefore, it's run after a field in the "Purchase Line" table is validated, after its value has been changed.

codeunit 50107 MyEventSubscriber
{
    // Update Purchase Line Jurisdiction Type once a Location Code change has been validated.

    [EventSubscriber(ObjectType::Table, Database::"Purchase Line", 'OnAfterValidateEvent', 'Location Code', false, false)]
    local procedure OnAfterValidateLocationCodePurchase(var Rec: Record "Purchase Line")
    begin
        UpdateJurisdictionType(Rec);
    end;
}

See also

AL method reference
Events in AL
Publishing events
Raising events
Subscribing to events
Method attributes