Enable the automatic numbering of a field on a page that adds records (back-end)

Advanced

Example. Enable the automatic numbering of the Code field on the page that adds a product. Use the following numbering pattern: ART_0000N, where N is 1, 2, etc. Implement the automatic numbering in the back-end.

1. Create system settings 

  1. Create a system setting that contains the product code pattern.

    1. Click to open the System Designer.
    2. Go to the System setup block → System settings.
    3. Click Add setting on the section toolbar.
    4. Fill out the properties of the system setting.

      • Set Name to "Product code mask."
      • Set Code to "ProductCodeMask."
      • Select "Unlimited length text" in the Type property.
      • Set Default value to "ART_{0:00000}."
  2. Create a system setting that contains the current product code.

    1. Click to open the System Designer.
    2. Go to the System setup block → System settings.
    3. Click Add setting on the section toolbar.
    4. Fill out the properties of the system setting.

      • Set Name to "Product last number."
      • Set Code to "ProductLastNumber."
      • Select "Integer" in the Type property.

2. Create a replacing object schema 

  1. Go to the Configuration section and select a custom package to add the schema.
  2. Click AddReplacing object on the section list toolbar.

  3. Fill out the schema properties.

    • Set Code to "Product."
    • Set Title to "Product."
    • Select "Product" in the Parent object property.
  4. Add an event to the schema.

    1. Open the Events node of the object structure.
    2. Go to the Adding block → select the Before record added checkbox. Creatio names the event ProductInserting.

    3. Click Save on the Object Designer's toolbar.
  5. Implement an event sub-process.

    1. Click Open process on the Object Designer's toolbar.
    2. Click System actions in the element area of the Designer and drag the Event sub-process element to the working area of the Process Designer.

    3. Set the Title property in the element setup area to "Product Inserting Sub-process."
    4. Set up the event sub-process elements.

      1. Set up the Message start event.

        • Set Title to "Before product adding."
        • Set Which message event should start the process? to "ProductInserting."
      2. Add the Exclusive gateway (OR) gateway .

        To do this, select Exclusive gateway (OR) in the menu of the Message start event.

      3. Add the Script task system action.

        1. Click System actions in the element area of the Designer and drag the Script task system action to the working area of the sub-process.

        2. Name the Script task system action "Get entity schema to generate number."
        3. Add the code of the Script task system action.

          Code of the Script task system action
          /* Set the schema that generates the number. */
          UserTask1.EntitySchema = Entity.Schema;
          return true;
          

          UserTask1 is the code of the Generate number system action of the User task type. The element setup procedure is covered on the next step. You can edit the code in the advanced setup mode of the User task system action.

        4. Click Save on the Process Designer's toolbar.
      4. Add the User task system action.

        1. Click System actions in the element area of the Designer and drag the User task action to the working area of the sub-process.

        2. Specify the properties of the system action.

          • Set Title to "Generate number."
          • Select "Generate ordinal number" in the Which user task to perform? property. The system action generates the current ordinal number based on the pattern specified in the ProductCodeMask system setting.
      5. Add the Script task system action.

        1. Click System actions in the element area of the Designer and drag the Script task system action to the working area of the sub-process.

        2. Name the Script task system action "Save number to entity column."
        3. Add the code of the Script task system action.

          Code of the Script task system action
          Entity.SetColumnValue("Code", UserTask1.ResultCode);
          return true;
          
        4. Click Save on the Process Designer's toolbar.
      6. Add the Terminate event.

        To do this, click Terminate in the element area of the Designer and drag the event to the working area of the sub-process.

    5. Set up the flows.

      1. Set up the conditional flow between the Exclusive gateway (OR) gateway and Get entity schema to generate number system action.

        1. Click in the menu of the Exclusive gateway (OR) gateway and connect the Exclusive gateway (OR) gateway to the Get entity schema to generate number system action.
        2. Fill out the conditional flow properties.

          • Set Title to "Code is empty."
          • Set up the Condition to move down the flow property.

            1. Click on the element setup area next in the Condition to move down the flow property.
            2. Specify the formula.

              string.IsNullOrEmpty(Entity.GetTypedColumnValue<string>("Code"))
              
            3. Save the changes.
      2. Set up the default flow between the Exclusive gateway (OR) gateway and Terminate event.

        1. Click in the menu of the Exclusive gateway (OR) gateway and connect the Exclusive gateway (OR) gateway to the Terminate event.
        2. Convert the sequence flow into a default flow. To do this, click in the flow menu → Default flow.

      3. Set up the sequence flows.

        1. Click in the menu of the Get entity schema to generate number system action and connect the Get entity schema to generate number system action to the Generate number system action.
        2. Click in the menu of the Generate number system action and connect the Generate number system action to the Save number to entity column system action.

    View the event sub-process in the figure below

  6. Click Save then Publish on the Process Designer's toolbar.

Outcome of the example 

To view the outcome of the example:

  1. Clear the browser cache.
  2. Refresh the Products section page.
  3. Add and save a new product. For example, Test product.

Creatio generates the code automatically and saves it to the column server-side when the Before Record Saved event is triggered. The event is triggered server-side when the query to add a record is sent from the front-end. As such, Creatio cannot display the code on the page that adds a product immediately. The number will be displayed when you save the product.

As a result, Creatio will enable the automatic numbering of the Code field on the product page.