{"title":"EventSubscriberInstance Property","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/properties/devenv-eventsubscriberinstance-property/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-eventsubscriberinstance-property, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-eventsubscriberinstance-property","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Specifies how event subscriber functions in a codeunit are bound to the codeunit instance and the events that they subscribe to.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Applies to","slug":"applies-to","at":189,"chars":24},{"level":2,"title":"Property value","slug":"property-value","at":213,"chars":316},{"level":2,"title":"Syntax","slug":"syntax","at":529,"chars":70},{"level":2,"title":"Remarks","slug":"remarks","at":599,"chars":3189},{"level":3,"title":"StaticAutomatic binding","slug":"staticautomatic-binding","at":1109,"chars":824},{"level":3,"title":"Manual binding","slug":"manual-binding","at":1933,"chars":1855},{"level":2,"title":"Example","slug":"example","at":3788,"chars":2248},{"level":2,"title":"Related information","slug":"related-information","at":6036,"chars":221}],"text":"> Version: _Available or changed with runtime version 1.0._\nSpecifies how event subscriber functions in a codeunit are bound to the codeunit instance and the events that they subscribe to.\n## Applies to\n Codeunit\n## Property value\nValue|Available or changed with|Description\nStaticAutomatic|runtime version 1.0|Subscribers are automatically bound to the events that they subscribe to.\nManual|runtime version 1.0|Subscribers are bound to an event only if the BINDSUBSCRIPTION method is called from the code that raises the event.\n## Syntax\n```al\nEventSubscriberInstance = StaticAutomatic|Manual;\n```\n## Remarks\nStaticAutomatic is the default value for this property. The bindings are always considered static on the event publisher side. For example, if you bind instance A of a given subscriber codeunit, all instances of publisher application objects will start calling the event subscribers. You can't design it so that a specific instance of publisher table X calls only a specific instance of subscriber codeunit A. You can, however, achieve the same by using/storing some global state on the subscriber.\n### StaticAutomatic binding\nIf the subscriber codeunit is defined as single instance (as specified by the devenv-singleinstance-property), the first call to an event subscriber creates in instance of the codeunit. This instance is kept for the lifespan of the session.\nIf the codeunit isn't declared as single instance, each event subscriber will be run in its own codeunit instance. When an event gets raised, a codeunit instance is created for each event subscriber method that subscribes to the event. The individual codeunit instances are immediately disposed after its event subscriber is run.\n> NOTE\n> With this setting, you can't call the ../methods-auto/session/session-bindsubscription-method or ../methods-auto/session/session-unbindsubscription-method for any events in this codeunit; otherwise, an error occurs.\n### Manual binding\nThis setting enables you to control which event subscriber instances are called when an event is raised. With this setting, you can essentially activate event subscribers on demand. If the BindSubscription method isn't called, then nothing will happen when the event is raised.\nThe binding of event subscribers to events is manual. To establish the binding, you must call the BindSubscription method to pass an instance of an event subscriber codeunit. Only then will the event subscribers be called.\nYou stop the event subscriber calls by either calling the UnBindSubscription method or by letting the previously bound instance go out of scope. For example, you declare a local variable, call BindSubscription, and then eventually exit the method that declares the local variable. All bindings on the instance are automatically unbound.\nBy controlling the instance of the subscriber codeunit, you control the lifespan. If you store the instance on a single instance codeunit global variable, you're effectively creating a binding that lasts the whole session. However, it's still only bound for the current session. Other sessions must also call BindSubscription to establish the direct binding to the instance of the subscriber codeunit.\n> IMPORTANT\n> When you develop on a production solution, you can experience a runtime error if the event subscriber codeunit has been updated by a development operation. For example, you recompiled the codeunit. This situation causes the event subscription codeunit's metadata to become stale and prevents the event subscriber from being called. A message similar to the following message appears in the event log:\n`A manually bound event subscriber is stale and will no longer be called. This can be caused by the subscriber object being updated by an operation in the development environment.`\n## Example\nThis code example uses the EventSubscriberInstance property to manually bind an event subscriber to an event. The example uses events to display a message to the user if they use a '+' sign in the Address field on the Customer Card page.\nThe following code creates codeunit that publishes the OnAddressLineChanged event.\n```al\ncodeunit 50100 MyPublishers\n{\n    [IntegrationEvent(false, false)]\n    procedure OnAddressLineChanged(line: Text[100])\n    begin\n    end;\n}\n```\nThe following code declares the CheckAddressLine event subscriber in the event subscriber codeunit 50101 MySubscribers. The event subscriber displays a message in the client when '+' is used in the Address field.\n```al\ncodeunit 50101 MySubscribers\n{\n    //Set the event subscribers to manual binding;\n    EventSubscriberInstance = Manual;\n\n    [EventSubscriber(ObjectType::Codeunit, Codeunit::\"MyPublishers\", 'OnAddressLineChanged', '', true, true)]\n    procedure CheckAddressLine(line: Text[100])\n    begin\n        if (STRPOS(line, '+') > 0) then begin\n            MESSAGE('Can''t use a plus sign (+) in the address [' + line + ']');\n        end;\n    end;\n}\n```\nThe following code extends the Customer Card page to raise the OnAddressLineChanged event when the Address field is changed:\n```al\npageextension 50100 MyCustomerExt extends \"Customer Card\"\n{\n    layout\n    {\n        modify(Address)\n        {\n            trigger OnBeforeValidate();\n            var\n                Publisher: Codeunit MyPublishers;\n                Subscriber: Codeunit MySubscribers;\n            begin\n                // Bind the event to the MySubscribers codeunit\n                BindSubscription(Subscriber);\n\n                // Raise the event\n                Publisher.OnAddressLineChanged(Address);\n            end;\n        }\n    }\n}\n```\nOn the event publisher side, the bindings are always considered static. For example, if you bind instance A of a given subscriber codeunit, all instances of publisher application objects will start calling the event subscribers. You can't design it so that a specific instance of publisher table X calls only a specific instance of subscriber codeunit A. However, you can achieve the same by using/storing some global state on the subscriber.\n## Related information\n../methods-auto/session/session-bindsubscription-method\n../methods-auto/session/session-unbindsubscription-method\n../devenv-publishing-events\n../devenv-raising-events\n../devenv-subscribing-to-events"}