Subscriptions to events 1c 8.2 processing of the holding. Assigning event handlers using event subscriptions. Instructions for using the "Event Study" program

When developing or modifying application solutions on the 1C:Enterprise 8.x platform, it is very often necessary to perform some standard action for a group of configuration objects (for example, directories). In order not to describe the actions being performed in the module of each object, the developer can use the standard platform mechanism - event subscription.

Event subscriptions allow you to intercept events of configuration objects, such as reference books, documents, plans for types of characteristics, and others. Today in the article we will consider the issue of the sequence of execution of event subscription handlers, and also analyze the behavior of the platform when there are several subscriptions to events for one action (for example, when recording).

Standard Behavior

Let in our example some directory "SimpleDirectory" is used. It has event subscriptions for each event that a developer can intervene in. The event handler procedures are located in the corresponding server common module.

The order in which subscription handlers are called is the same as in the standard behavior of the platform when working with a given object. Since in our example we are considering working with a reference book, I propose to consider the scheme for calling handlers depending on actions with an object (see the following screenshot).

As we can see, at the initial stage, the event handlers "ProcessingFill" (to create a new element) or "OnCopy" (to create an element based on an existing one) are called. In both cases, after calling the named handlers, the "OnSettingNewCode" procedure is executed, where the developer can set a prefix in the code or override the platform's behavior when assigning new code.

When writing a directory element, whether it is a new element or an existing one, three handlers are called: "ProcessingFill Check" (at this stage, the handler can check the correctness of the entered data and, if there are errors, refuse to write), "BeforeWrite" (until the object is written to the database, you can adjust the values ​​of the details and check any additional conditions) and then "On Write" (the record was made to the database, but the transaction is not closed, the developer can check the data after the record and, if necessary, cancel the transaction).

The "BeforeDeletion" event occurs only if the object is directly deleted from the infobase. Normally, no user has direct deletion rights without a referential integrity check. Deletion should always be done by "Deleting Marked Objects" processing. In the latter case, the "BeforeDelete" handler is also called.

Thus, if we create a directory element and write it to the infobase, the platform will call the following event handlers in the specified order:

Regarding other configuration objects, the operation of the event subscription mechanism will be similar, only the events and their order may differ. See the syntax helper for details.

Undocumented side

Now consider an interesting situation. Let's say for our reference book "SimpleCatalog" three subscriptions to the "BeforeWrite" event are defined:

In what order do you think the handlers of these subscriptions will be called? Let's not guess. I will give the result of writing an element, where the handler of each subscription displays a message with the name of the called subscription (see the following screenshot).

From the screenshot, it is not difficult to guess that the order of calling the event subscription handler procedures corresponds to the order of the metadata objects in the "Event Subscriptions" branch. This feature is not described in any reference literature on the 1C:Enterprise platform, so you should be careful when using it and in the configuration, since undocumented features may change from version to version of 1C:Enterprise and, at the same time, may not be in the list of program changes.

Retreat

You ask: "Why create multiple subscriptions for a single configuration object event?". The answer is simple. If several people are involved in the development, then interference in the created mechanisms of each other can lead to incorrect operation of the program. In such cases, it would be most logical to create separate event subscriptions for each developer in accordance with the task. Of course, it is possible that in the future they will be merged into a single handler procedure.

This article is an announcement of new functionality.
It is not recommended to use the content of this article to learn new functionality.
A full description of the new functionality will be provided in the documentation for the respective version.
The full list of changes in the new version is given in the v8Update.htm file.

Implemented in EDT version 1.7.0.567.

In 1C:Enterprise Development Tools (EDT) we implemented a prototype of a new tool. The working title of this tool is Editor All event subscriptions. It will help you conveniently analyze subscriptions to all events that exist in the application solution.

Event Subscriptions

The 1C:Enterprise platform allows you to create subscriptions to events of configuration objects in an application solution. A subscription is a procedure that will be executed after the original event handler has executed. The convenience of subscriptions lies in the fact that one procedure can be “subscribed” to an event belonging to different configuration objects. Thus, if there is an algorithm that needs to be executed both when recording an organization and when recording a department, it can be placed in a subscription, and then you don’t even have to change the handlers for this event in the objects themselves.

It turns out that the subscription is a convenient and universal mechanism. But in large application solutions, the number of event subscriptions can reach several hundred. It becomes inconvenient to analyze them in the configuration tree, in a linear list. For example, in the applied solution 1C: Enterprise Management (ERP) over 340 event subscriptions.

EDT makes it a little easier to work with subscriptions by showing them in the panel Scheme when the module of any application object is opened.


This display of subscriptions is convenient for a number of tasks related to module editing. But it is still not suitable when you need to quickly find and analyze all the algorithms that are executed in subscriptions when an event occurs.

All event subscriptions

To get rid of the inconveniences listed above, we have implemented a generic way to represent subscriptions, events, configuration objects, and procedures that implement subscription algorithms.


As a result, you can call the editor All event subscriptions for the entire configuration, or only for one object - the difference will be only in the composition of the data filtered in some way.


On the left side, the editor shows all events, and in each event, all its subscriptions. When a specific subscription is selected, the list of configuration objects to which the subscription is “subscribed” is displayed at the top right. And at the bottom right, the module and the procedure in which the subscription algorithm is located are shown. By double-clicking on a procedure, you can open it in the built-in language editor.

While in the editor, you can analyze not only individual subscriptions, but also all subscriptions related to the same event. If you select an event, the editor will show all modules and all procedures subscribed to handle this event.


If you invoke the editor on a configuration object, only that object's events and subscriptions will be shown, and the object itself will always be highlighted in red in the source list. Thus, you can quickly check, for example, that the subscription you choose works for all configuration objects that need it.


Invoking the editor with a context command (on the configuration object) allows you to immediately reduce the number of subscriptions displayed in the editor. For example, you can view subscriptions only for those events that are processed in the object module or in the manager module.


In addition, the editor contains a universal filter with which you can arbitrarily customize the composition of objects, events and procedures.


Note that with this filter, you can select not only specific objects that are the source of events, but also sets of types, such as DirectoryObject, DocumentObject and others. Such type sets include all directories or all documents that are in the configuration.

With a string search, you can quickly find only those subscriptions that apply to the engine you are interested in.


At any time, you can quickly filter content based on the event or source shown in the editor. For example, you found a subscription Check Formula Calculation. Its source is the plan of calculation types Holds.


With the help of the contextual command on the plan of calculation types, you can quickly see only those subscriptions that are associated with its events.


Automatic addition of breakpoints

One common way to analyze event subscriptions is to sequentially view all called procedures in the debugger in the order in which they were executed. To do this, the editor provides a convenient tool for automatically adding breakpoints to handlers.

First of all, you can call this tool right in the editor.


You can find and select the object you are interested in, select one of its events, and mark, for example, all handlers. After pressing OK breakpoints will be added to the first executable line of each checked handler, and all these breakpoints will appear in the panel Breakpoints in perspective Debugging.


Another way to add breakpoints is convenient when you have already found the object or event you are interested in in the editor. In this case, you can call the command that suits you from the context menu.


And finally, the third way that you can use is to automatically add breakpoints already in the process of debugging. In this case, you do not need to open the editor, because the add command is located directly in the panel Breakpoints.


So the editor All event subscriptions is a versatile tool that allows you to use a variety of analysis scenarios. It will be useful not only for developers who know the applied solution well, but also for implementation specialists or IT specialists who need to deal with unfamiliar functionality.

In the course of solving various tasks of users, it sometimes becomes necessary to subject already formed movements of documents (namely, certain sets of registers) to some kind of adjustment.

For these purposes, the “Event Subscription” object is very suitable, which allows you to perform some actions when a certain event occurs for a large number of objects (for example, when recording payment documents or when setting a new number of directories related to tax accounting).

Also, subscribing to an event is convenient because it allows you to perform various actions without changing the typical mechanisms described in different modules.

For example, a task arose - it is necessary to record certain data (information about the company's activities) in payment documents after the formation of the main movements of the document (formed in the "Processing" event). The task will be implemented on the configuration "Manufacturing Enterprise Management" ed. 1.3.

Let's look at the solution in more detail:

Let's create a new subscription to the "Record Direction to Payment" event. A subscription has a number of properties that will determine its behavior:

Source is an object (for example, a document or a list of documents) for which the action will be called. For our case, we choose Outgoing payment order and Incoming payment order

Events- the action itself, after which our code will be executed. According to the conditions of the problem, we choose Processing

Handler- an indication of the procedure in which the processing will take place. For these purposes, we choose the general module General Purpose.

After the above goals, a procedure is created in which it is necessary to place a code to fill in the data on the direction (assume that such information is already contained in the payment orders).

Consider its parameters:

Source- this object of the type DirectoryObject or DocumentObject for which the action takes place.

Refusal- a parameter that allows you to cancel posting a document under certain conditions.

Holding mode- options for conducting (operational or non-operational), allowing you to build processing algorithms in different ways.

Let's stop at the parameter Source. For our task, the type of this parameter will be - DocumentObject. A collection is available for this type movements A that contains all register record sets for which this document is the registrar.

This collection contains a set of records SettlementsWith CounterpartiesSetRecords which interests us. Let's assume that the dimension Direction is created in the register, which we need to fill out from the document.

Let's write the following code:

Sets = Source. movements; Calculations = Sets. Settlements with contractors; For each Page from Calculations Cycle Page. Direction = Source. Direction; EndIf ;

As we can see, the implementation is quite simple, after processing the action to write the set, you do not need to take an additional action - the subscription to the event is performed as part of the ProcessingPerforming event transaction, after its completion the set will be written automatically.

The advantages of this approach: processing data outside of standard algorithms, reducing the amount of work on finding and transferring changes during updates, greater visibility - all the code in one procedure.

Downside of this approach: an increase in the time for conducting documents and recording elements of directories.

I hope this information will be useful for both novice programmers and their more experienced colleagues as an extension of their horizons.

When a user performs any actions, the 1C platform generates program events. As a rule, not one event is generated, but a whole chain of events. The task of the programmer is to correctly place the program code in events in order to achieve the expected behavior from the program. However, it will not be easy for a novice 1C programmer to do this, for the reasons listed below.

Events can be generated in a managed form: OnReadOnServer, OnCreateOnServer, OnOpen, etc.

Events in a managed form are generated on the client and on the server: BeforeWrite, BeforeWriteOnServer.

Events are called in different modules: ItemForm, ObjectModule, ManagerModule.

Some events can be called several times if there are several reference items in the list, for example: ProcessingViewReceiving.

A managed form can be opened as a result of performing different user actions, while the chains of calling events will differ. For any of the following user actions with the directory, a managed form will be opened: creating a new element, copying an element, changing an existing element of the directory.

Events are also generated by form elements: when a row is added to a tabular section, when a row in a tabular section is edited, when a row or field is activated, when a lookup element is selected in the input field, etc.

To better understand the logic and sequence of triggered events, you can use the "Event Study" development attached to this article. Knowing the context of the event, the sequence of events and the actions that the user will perform, it will be easier to understand which event handler is best to place his program code.

Instructions for using the "Event Study" program

The "Event Study" program shows the events that the 1C platform generates during interactive user actions. The principle of operation is as follows, the user opens the directory, the program shows a chain of events. The user marks a directory element for deletion, the program displays a sequence of events that occur. Events are displayed with a short default delay of 3 seconds, this is necessary to separate one event chain from another event chain. Therefore, you need to perform interactive actions “slowly”.

All events are displayed in a special window "Latest events". It allows you to enable or disable event recording. By default, when you open it for the first time, event logging is enabled. I advise you to fix the “Last events” window at the bottom of the screen immediately when the program starts, for convenient viewing of events.

The program itself cannot determine what action caused the chain of events, I advise you to type the names of your last actions from the keyboard in the “Action reason” field, for example, “The directory list form is open”, “An element in the directory list is marked for deletion”, etc. This will facilitate later analysis of actions and events.

Events are recorded and displayed for objects placed in the Event Tracing section, provided that event recording is enabled in the Recent Events form.

All recorded events can be viewed through the "Event Report", which is located in the "Service" section.

To quickly clear all recorded actions and events in the “Service” section, select “Clear events and actions”.

The event subscription mechanism is designed to assign an event handler to one or more 1C:Enterprise configuration objects. The article discusses several examples of the application of this mechanism. After reading the article, you will learn:

  • What is an event subscription and how to use it in practice?
  • How to check for duplication of the name when writing a directory element without modifying the modules of the directory itself?
  • How, using a subscription to an event, to ensure the formation of movements in the accumulation register when posting a document?
  • How to ensure the substitution of the main form of the document?

Applicability

The article discusses the 1C:Enterprise platform, version 8.3. The information provided is relevant for current releases of the platform.

Event Subscriptions

The article discusses several examples of using one of the auxiliary objects of the 1C:Enterprise 8 platform - event subscriptions.

Event subscriptions allow you to place external handlers in shared modules, which will be executed after the execution of a specific event handler in an object module or manager module.

Wherein not required make changes to the object module or manager module. Thus, the possibility of software extensions of modules without their modification- This is a very useful technique when changing standard solutions.

Event Subscriptions described in thread Are common configuration object windows (Fig.1).

If the configuration creates a subscription to the event of some object, for example, the event BeforeWrite() document object, then when this event occurs, the platform performs the following sequence of actions.

  1. The event handler is executed BeforeWrite() in the document object module.
  2. If during the execution of the handler the parameter Refusal takes on the value True or an exception is thrown, the event handling is aborted.
  3. If the event processing was not interrupted at the second step, then external handlers (subscriptions to events) defined for the event are executed. BeforeWrite().
  4. If during the execution of the external handler the parameter Refusal takes on the value True or an exception is thrown, then execution of the outer handler is aborted.

With event subscriptions, you can organize performing various checks, which are executed when objects are written to the database.

Task 1

Check for duplication of the name when writing the element of the "Contractors" directory - without modifying the modules of the directory itself.

To solve the problem, you need to create a common module EventSubscriptionHandlers. Set the flag in the module properties palette Server and Client (regular application). The second flag is needed in order for the event subscription to work in a normal application.

Flag Client (regular application) available if edit mode is set in the configurator parameters Managed Application vs Regular Application.

In branch Are common the configuration objects window create a new event subscription. Enter the name of the subscription in the properties palette Checking the Directory Name. In the selection box Source mark data type DirectoryObject.Contractors. In the selection box Event select an event BeforeWrite(). After processing this event, the event subscription processing procedure will be triggered (Fig. 2).

In the selection box Handler the general module is specified, in which the event subscription handler is located. Click the Open button in this field, select the module EventSubscriptionHandlers and press OK. The system will automatically create a procedure in the common module with parameters Source And Refusal. In parameter Source the object for which the event subscription is created is passed - DirectoryObject. . In parameter Refusal a sign of refusal to write the element is transmitted.

In procedure CheckingDirectoryNameBeforeWriting() a query is made to the directory Counterparties. The name of the recorded dictionary element is passed as a query parameter Counterparties. If an element with the same name already exists in the database, then the parameter Refusal set to value True(element entry is canceled) and the corresponding diagnostic message is displayed.

Procedure listing CheckingDirectoryNameBeforeWriting()

In practice, you may encounter the task of performing movements on additional registers when posting documents in standard configurations. The creation of additional registers makes it possible to avoid modifying existing registers and, at the same time, to obtain the possibility of additional data processing when conducting standard documents.

Task 2

Create a turnover register of accumulation “Disposal of funds” and ensure the formation of movements on this register when posting the document “Expenditure cash order” using the event subscription mechanism.

Create a new turnover register with the name Cash Disposals. Select the registrar "Expenditure cash order". Add register dimensions:

Create register resource:

Sum, type Number, Length – 15, Accuracy – 2.

In the document “Expenditure cash warrant” create a requisite ArticleMovement with data type HandbookLink.ArticlesCash Movements.

Name - Cash Disposal Movements;
Source - DocumentObject.RKO;
Event - Processing.

In the general module EventSubscriptionHandlers create a handler . The handler bypasses the tabular part of the document “Expenditure cash order” and generates movements in the accumulation register Cash Disposals.

Procedure listing Cash Disposal MovementsProcessingConducting()

In typical configurations, it may be necessary to refine the main form of some object, such as a document. This task can be solved using event subscriptions. This creates a copy of the main form of the document. The necessary changes are made to the new form. Using the event subscription mechanism, a new form is opened instead of the main form. In this case, the main form, which is on support, remains unchanged.

Task 3

Provide a replacement for the main form of the document “Expenditure cash warrant”.

Create a new form of the document “Cash Expense Order” with the name FormDocumentClient. Make arbitrary changes to the form, for example, change the order of controls. To call this form, you must use an event subscription HandlingGettingForm() in the module of the document manager “Outgoing cash order”.

Create a new event subscription:

Name - Basic Form RKO;
Source - DocumentManager.RKO;
Event - ProcessingReceivingForm.

In the general module EventSubscriptionHandlers create a handler . To the handler as a parameter SelectedForm the name of the opened form is passed.
Parameter Standard Processing is set to Lie to disable opening the main form.

Procedure listing MainFormRKOProcessingGettingForm()

To search for event subscriptions defined for some configuration object, you can use the object reference search mechanism. To do this, select an object in the configuration objects window and execute the command in the context menu Finding references to an object. As a result of executing the command, a list of objects containing links to the searched object will be displayed in the service message window.

Thus, event subscriptions provide the ability to add new functionality without changing existing object modules. The disadvantages of event subscriptions include:

  • Increasing the complexity of algorithms.
  • You can only subscribe to events of objects and object managers.

If you need to modify any form event, then the event subscription mechanism is not available. In this case, you need to make changes to the form itself or copy the form and make changes to the new object.



Have questions?

Report a typo

Text to be sent to our editors: