How to add auto-numbering to the edit page field
Glossary Item Box
One of the typical configuration tasks is the setup of auto-numbering for a specific field of the edit page. The examples may be the [Documents], [Invoice], and [Contracts] sections where a newly created document receives an automatically generated number of a certain format.
To implement the auto-numbering task, we recommend to override the onEntityInitialized base virtual method and call the getIncrementCode method of the BasePageV2 card base schema in it.
The getIncrementCode method has two parameters:
- [callback] — a function to be called after a response from the server. The server response must be passed to the field which requires assigning the automatically generated value;
- [scope] — the context where the [callback] function will be called (unrequired parameter).
To use the auto-numbering mechanism, two system settings must be added:
- [Entity]CodeMask — an object number mask,
- [Entity]LastNumber — the current object number,
where [Entity] — the name of the object for whose column the auto-numbering will be used, for example, InvoiceCodeMask (Invoice number mask) and InvoiceLastNumber (Current invoice number).
For the auto-numbering to operate, not only on the client side but also on the server side (for example, to implement tasks for the bpm'online system integration with other systems), the [Before Record Adding] event handler must be added to the object for whose column the auto-numbering will be used. The following number generation parameters must be set in the business process. Specify the object schema for which the generation will be performed. Then, call the [Generate Number of Order] action. Finally, pass the generated value to the required column of the object.
Procedure for adding the auto-numbering:
- Add system settings for the object number mask and current object number.
- Override the onEntityInitialized base virtual method and call the getIncrementCode base method, specifying the field for which the number will be generated.
- Add the [Before Record Adding] event handler to the object.
Below is the example of adding the auto-numbering to the edit page field.
Case description
Set the auto-generation in the [Products] section for the [Code] field of the edit page. The product code must be formed as follows — ART_00000.
Case implementation algorithm
1. Create two system settings
Create the ProductCodeMask system setting with the number mask “ART_{0:00000}” (Fig. 1).
Fig. 1. — [Product code mask] system setting
Create the ProductLastNumber system setting (Fig. 2).
Fig. 2. — [Current product code] system setting
2. Create the [Products] replacement edit page in a custom package
A replacement client module must be created and the [Edit Page - Product] must be specified as the parent object (Fig. 3).
The procedure for creating a replacement page is described in the article Creating a custom client module schema.
Fig. 3. — Properties of the [Products] replacement edit page
3. Override the onEntityInitialized method in the collection of methods of the edit page view model
Call the getIncrementCode method in the onEntityInitialized method and specify a column for the [Code] auto-generation in its callback function.
define("ProductPageV2", [], function () { return { // Name of the edit page object schema. entitySchemaName: "Product", details: /**SCHEMA_DETAILS*/{}/**SCHEMA_DETAILS*/, diff: /**SCHEMA_DIFF*/[],/**SCHEMA_DIFF*/ // Methods collection of the view model page. methods: { // Redefining the Terrasoft.BasePageV2.onEntityInitialized Base method which actuates // upon the completion of the initialization of the edit page object schema. onEntityInitialized: function () { // The code is generated only if a new item is generated or the existing item is copied. if (this.isAddMode() || this.isCopyMode()) { // Calling the Terrasoft.BasePageV2.getIncrementCode base method which generates a number // based on the pre-set mask. this.getIncrementCode(function (response) { // The generated number is returned to the [Code] column. this.set("Code", response); }); } // The parent implementation of the method is called onEntityInitialized. this.callParent(arguments); } } }; });
4. Add the [Before Record Adding] event handler of the object
For this purpose, the [Product] replacement object must be created:
- Select a custom package and run the menu item [Add] > [Replacing Object] on the [Schemas] tab.
- Fill in properties of the new object by specifying [Product] as the parent object.
The [Before Record Adding] event handler must be added to the created object, in the list of events (Fig. 4).
Fig. 4. — Event sub-process — [Before Record Adding] event handler
Items of the [Before Adding] event sub-process:
1. [Before Adding] start message — this sub-process will be started when the [ProductInserting] message is received.
2. [Exclusive OR] Gateway from which two flows of the sub-process come:
- Default flow will be activated only if the passage by the conditional flow cannot be performed. This leg is finished by the [Stop] event .
- [Code is not Filled] conditional flow, wherein the the [Code] column is verified. Further implementation of the sub-process is possible only if it is filled in:
// Code to be added to the [Condition] field of the conditional flow. string.IsNullOrEmpty(Entity.GetTypedColumnValue("Code"))
3. Script task [Script task [Set Number Generation Parameters ] — the C# script software code will be executed in it:
// Setting the schema for a number generation. // GenerateNumberUserTask is the [Generate Number] action. GenerateNumberUserTask.EntitySchema = Entity.Schema; return true;
4. [Generate Number] process action — the [GenerateNumberUserTask] system action which directly generates a current sequence number in accordance with the set mask.
5. Script task [Set the Received Number] — the C# script software code will be executed in it:
// The value generated by the GenerateNumberUserTask system action // is recorded into the [Code] column. Entity.SetColumnValue("Code", GenerateNumberUserTask.ResultCode); return true;
5. Save the replacement page schema and publish the replacement object.
When the schema is saved, the object is published and the system web-page is updated, a generated code will be displayed automatically on the product edit page when adding a new product to the [Code] field (Fig. 5).
Fig. 5. – Result demonstration