1C:Enterprise 8.3. Practical Developer’s Guide. Lesson 21 (0:40). Editing register records in document forms. Editing register records from 1C:Enterprise script

Editing register records from 1C:Enterprise script

Writing register records from a form

Writing register records from 1C:Enterprise script

If you run the Write() method of the document object, the document is written without involving the document form. This means that the BeforeWrite event of the document form is never called and so the handler script is never executed.

To synchronize the period of document register records with the document date in the scenario where the document is written using 1C:Enterprise script tools, you have to use the BeforeWrite event handler of the document object instead of the BeforeWrite handler of the document form.

During interactive document writing the BeforeWrite event of the document form is called first, and then the BeforeWrite event of the document object is called (see the chart of events in section Event sequence for writing documents from document forms).

Choosing handler locations

We recommend that you choose the handler to store your custom algorithm depending on the object logic. If your configuration does not include the option to save the object using 1C:Enterprise script tools, you can choose the form module handler. But if your configuration includes 1C:Enterprise script algorithms that save the object, choose the object module handler.

Note that neither of these methods prevents modification of register records using the
<...>Register.RecordSet.<register name> object. Therefore, if the configuration logic suggests possibility of script-based modifications of a RecordSet object, the handler code should be located in the event handler of this record set. All attempts to modify register data ultimately come down to writing a record set.

In Designer mode

The first requirement that you will implement is that register records must be generated with the same date as the date of the document. In other words, let us synchronize the register records date with the document date.

  • Create the BeforeWrite client event handler for the document form and add the following script to this handler (listing 21.1).

    Listing 21.1. BeforeWrite event handler of the document form

    For Each RegisterRecord In Object.RegisterRecords.BalanceOfMaterials Do
        RegisterRecord.Period = Object.Date;
    EndDo;

In 1C:Enterprise mode

Let us check the document.

  1. Start 1C:Enterprise in the debug mode.
  2. Open the document and click Save.
  3. Open the records created by the document in the BalanceOfMaterials register.

    You can see that the value of the Period field of all the records is now equal to the document date (fig. 21.7).

    Lesson 21 (0:40). Editing register records in document forms / Editing register records from 1C:Enterprise script / Writing register records from a form / In 1C:Enterprise mode
    Fig. 21.7. Modified records of the BalanceOfMaterials register

    So you achieved the goal, but only for scenarios where the document is saved interactively.

In Designer mode

Let us implement the event handler in the InputOpeningMaterialBalances document object module.

  1. Return to Designer and comment out the handler script in the form module.
  2. In the InputOpeningMaterialBalances document object editor, on the Other tab, open the object module and add the script shown in listing 21.2.

    Listing 21.2. BeforeWrite event handler of the object module

    Procedure BeforeWrite(Cancel, WriteMode, PostingMode)
     
        // Determining whether updating register record dates is required
        UpdateRegisterRecordsDate = IsNew() Or RegisterRecords.BalanceOfMaterials.Modified();
        If Not UpdateRegisterRecordsDate Then
     
            // Verifying that the date changed
            Query = New Query;
            Query.SetParameter("CurDocument", Ref);
            Query.Text =
                "SELECT 
                |    Date
                |FROM
                |    Document.InputOpeningMaterialBalances
                |WHERE 
                |    Ref = &CurDocument";
     
            Selection = Query.Execute().Select();
            Selection.Next();
            UpdateRegisterRecordsDate = Selection.Date <> Date;
        
        EndIf;
     
        // Assigning the new date to all records, if required
        If UpdateRegisterRecordsDate Then
     
            If Not RegisterRecords.BalanceOfMaterials.Selected() And Not
                RegisterRecords.BalanceOfMaterials.Modified() Then
    
                RegisterRecords.BalanceOfMaterials.Read();
    
            EndIf;
     
            For Each RegisterRecord In RegisterRecords.BalanceOfMaterials Do
                RegisterRecord.Period = Date;
            EndDo;
     
        EndIf;
     
    EndProcedure
    As you can see, in this case the handler contains more lines, due to the additional checks that are performed because both interactive and script-based writing of the object is possible.

    Let us walk through the handler contents in greater detail. If a new document is written or its register records are changed, you need to update the register record dates. Otherwise the query reads the document date from the database and then the date is compared to the date of the object being written. If the dates do not match, you also need to update the register record dates.

    Before a date is set, the script checks whether a record set was read from the RegisterRecords property of the object and whether it has been modified. If both conditions are false, it means that the record set in the RegisterRecords property of the object is empty and that this condition is not due to its modification. In this case, to prevent erroneous deletion of register records (overwriting them with an empty record set), the script preliminary reads the register records from the register into the RegisterRecords property.

    Then, as in the previous scenario (writing register records from a form), the required date is assigned to all the records in that set. When the document object is recorded, this record set is written to the accumulation register.

In 1C:Enterprise mode

Let us check the new event handler.

  1. Start 1C:Enterprise in the debug mode.
  2. Set a new date for the document (for example, 02/03/2015), save it, and ensure that the records in the accumulation register have the new date as well.

While saving a document, you can control not only the period of the accumulation register records, but also the values of other register fields.

For instance, by applying the same principle you can create an AccountingOperation document for entering manual operations into the accounting register.

In that case, in addition to controlling the period of register records, you need to manage the values in the Active field (enabling or disabling document posting), and so on.

Leave a Reply

Your email address will not be published.

 

1C:Enterprise Developer's Community