Calling the function upon material or service selection for filling the price
Your next task is implementing automatic filling of the Price field once a user selects a service in the Services document. And the service price must be based on the document date.
- In Designer, find the Services document and open its DocumentForm.
- Double-click the MaterialsAndServicesMaterialOrService form item.
Right-click the MaterialsAndServicesMaterialOrService form item and then click Properties.
This opens the property palette of that item.
- Scroll down the property palette to find the OnChange event.
This event occurs when the field value is changed.
- In the OnChange field, click the Open button .
- In the window that prompts you to select the handler type, keep the default Create on client option (fig. 9.10).
This option indicates that you are creating a client handler of an event that is triggered by interactive user actions.
Fig. 9.10. Selecting handler type for a form event
The platform generates a handler procedure template for this event in the form module and opens the Module tab of the form editor.
- Add the following script to the handler procedure body (listing 9.2).
Listing 9.2. Procedure MaterialsAndServicesMaterialOrServiceOnChange()
// Getting current tabular section row TabularSectionRow = Items.MaterialsAndServices.CurrentData; // Setting price TabularSectionRow.Price = CatalogProcessing.RetailPrice(Object.Date, TabularSectionRow.MaterialOrService); //Recalculating row total DocumentProcessing.CalculateTotal(TabularSectionRow);
Now let us examine this script.
You are already familiar with the first line of the handler after the MaterialsAndServicesQuantityOnChange and MaterialsAndServicesPriceOnChange procedures. It gets the current tabular section row of the document and stores it to the TabularSectionRow variable for future use.
The next line calls the RetailPrice() function from the CatalogProcessing common module.
The first function parameter is the document date, which is used to retrieve the price. The date is taken from the Object.Date default form attribute.
The second parameter is a reference to an item of the MaterialsAndServices catalog that is stored in the current tabular section row (TabularSectionRow.MaterialOrService).
The function returns the latest price value, which is assigned to the Price field of the current tabular section row (TabularSectionRow.Price).
The next line calls the CalculateTotal procedure from the DocumentProcessing common module. It is the procedure that you created earlier for recalculating the document row total when the quantity or price is changed.
Note that the MaterialsAndServicesMaterialOrServiceOnChange procedure execution starts in the form module on the client because it is a handler of an interactive form event. During the generation of the procedure template the platform added the &AtClient compilation directive before the procedure description.
Then the RetailPrice() function is called. Since this function is not found on the client, the execution is passed to the CatalogProcessing common module that is executed on the server. Once the function execution is completed, the script execution continues on the client.
So why do we need this trick? Why is it necessary to pass the script execution to the server?
It is because all database operations (reading or writing data) can only be performed on the server. Here you need to read the latest data for some material or service from the information register.
This action can only be performed on the server, and if you check the description of the GetLast() method of an information register in the Syntax Assistant, you will see that this method is only available on the server, in the thick client, and in external connections.
Thick client and external connection are client applications of the previous platform version that are supported for compatibility with legacy applied solutions.
But you are developing a new applied solution that uses the thin client or the web client. So in order to get some data from the database you need to pass the script execution to the server, get the required data there, and return the data to the client. And this is exactly what the handler does.