1C:Enterprise 8.3. Practical Developer’s Guide. Lesson 4 (1:30). Documents. A single procedure as a hander for multiple events

A single procedure as a hander for multiple events

So you checked that once you change the quantity in any row of a Goods receipt document, the platform automatically recalculates the total.

You also need the Price field to behave in a similar manner. Taking a look ahead though, you will see that similar automatic calculation of Total fields might come in handy for other documents as well.

Therefore, it is better to move the calculation of totals to some universally accessible location, so that various documents with similar attributes in their tabular sections will be able to use the algorithm.

The Common module configuration objects serve for defining these universally accessible locations. You can add them in the Common / Common modules branch of the configuration object tree. The procedures and functions stored in these modules are accessible from all the configuration objects.

So you will create a common module and move the total calculation procedure to this module. And in the document you will simply call that procedure from the common module.

In Designer mode

In 1C:Enterpise mode

Start 1C:Enterprise in the debug mode and ensure that the tabular section row totals in Goods receipt documents are recalculated in response to changes in both quantity and price.

Common module

Let us add a Common module configuration object.

  1. In the configuration object tree, expand the Common branch by clicking the Lesson 4 (1:30). Documents / A single procedure as a hander for multiple events / In Designer mode / Common module icon to the left of the branch name.
  2. Right-click the Common modules branch and click the Add Lesson 4 (1:30). Documents / A single procedure as a hander for multiple events / In Designer mode / Common module button in the command bar of the configuration window (fig. 4.24).

    Lesson 4 (1:30). Documents / A single procedure as a hander for multiple events / In Designer mode / Common module
    Fig. 4.24. Creating a common module in the configuration object tree

    This opens the window where you can enter the module text and the module property palette.
  3. In the property palette, name the module DocumentProcessing, select the Client (managed application) check box, and clear the Server check box.

    It means that instances of this module will be compiled in the thin client context and in the web client context (fig. 4.25).

    Lesson 4 (1:30). Documents / A single procedure as a hander for multiple events / In Designer mode / Common module
    Fig. 4.25. Common module properties
  4. Add the following script to the module (listing 4.2).

    Listing 4.2. Procedure CalculateTotal()

    Procedure CalculateTotal (TabularSectionRow) Export
        TabularSectionRow.Total = TabularSectionRow.Quantity * TabularSectionRow.Price;
    EndProcedure
    Now let us examine this script. The TabularSectionRow variable that you defined in the OnChange event handler of the Quantity field is passed to the CalculateTotal() procedure. This variable stores data of the tabular section row of the Goods receipt document that is being edited.

    You can use this variable to access tabular section column data and calculate the total by multiplying the price and quantity.

    The Export keyword in the procedure title indicates that the procedure can be accessed from other applied solution modules.
  5. Change the handler script in the form module (listing 4.3).

    Listing 4.3. Procedure MaterialsQuantityOnChange()

    &AtClient
    Procedure MaterialsQuantityOnChange(Item)
        TabularSectionRow = Items.Materials.CurrentData;
        DocumentProcessing.CalculateTotal(TabularSectionRow);
    EndProcedure
    You can see that the first line of the procedure remains unchanged. And in the second line instead of directly calculating the total the CalculateTotal() procedure from the DocumentProcessing common module is called. The current tabular section row is passed to the procedure as a parameter.
  6. Start 1C:Enterprise in the debug mode and ensure that nothing has changed from the user perspective.

    Finally, let us implement a similar handler for the Price field. Since you have already coded the required procedure in the form module, you might want to reuse it with a different form control. However, the 1C Company configuration development standards do not permit that approach.

    Learn more! According to the 1C Company development standards, each event must have its own handler. If identical actions are to be performed in response to changes to different controls (for example, in response to clicking any of the several buttons), these should be handled as follows:
    • A dedicated procedure or function that performs the required actions is created.
    • For each control, a separate handler with a default name is created.
    • The procedure or function is called from each handler.
    This is why you will create the OnChange event handler for the MaterialsPrice field of the tabular section in the same manner as you have done it for the MaterialsQuantity field and repeat the call of the CalculateTotal() procedure from the common module.
  7. Add the OnChange event with the following script to the Price field (listing 4.4).

    Listing 4.4. Procedure MaterialsPriceOnChange()

    &AtClient
    Procedure MaterialsPriceOnChange(Item)
        TabularSectionRow = Items.Materials.CurrentData;
        DocumentProcessing.CalculateTotal(TabularSectionRow);
    EndProcedure

Leave a Reply

Your email address will not be published.

 

1C:Enterprise Developer's Community