Tips & Tricks - Unique number generator

The unique number generator makes it possible to generate automatically incrementing numbers according to predefined criteria. In these cases, the number can consist of any required combination of parts (number, date, time, word). The best known examples of unique numbers are probably inventory numbers. When creating a data record for an item of stock in a corresponding Intrexx application, you can use the unique number generator to generate numbers that are continuous and incremental, and are constructed according to a defined syntax (e.g. Inv-2007-06-0001, Inv-2007-06-0002, etc.).

It also makes sense to use the unique number generator for orders, parts lists or customer numbers. Unique numbers can be generated via the data handler of a data field. A check is made before the record is saved, to determine which unique number in the previously saved data records has the highest allocated number at that point in time. When the data record is saved, a new number – the previous highest plus 1 – will be written to the data field. A sequential number assignment is guaranteed with this method. Even if the portal service is restarted, the highest allocated unique number will be determined and used as the basis for calculating the subsequent numbers.

Automatically incrementing numbers can also be generated via Groovy. JavaScript and Velocity are not suitable for this because when the page is loaded, it is not certain whether it will also be saved.

Configure the datahandler

First, create a new application in the Portal Manager based on the "Basic application" application template. Then create a new data field in the data group contained there. Since values with Date, Integer and String format can be included in a unique number, please use data fields with the "Short text" data type only. Do not place a unique number in a system data field. Only use data fields that you have created yourself. Assign the title "Unique number" here.

Display the data fields in the application structure by selecting "Show data fields" from the Edit menu. Please activate the expert options if you have not done so already.

Open the properties dialog of the Unique Number data field by double-clicking on the data field in the application structure and there go to the "Expert" tab. Enter


as the value of the "datahanlder" attribute and click on "OK".

Now you can include the "Unique number" field in the table on the All Entries page. Save the application. Save the application.

Create the UniqueNumber.xml file

The UniqueNumber.xml file contains the definition of all unique numbers in the current portal.

<?xml version="1.0" encoding="UTF-8"?>
	<uniquenumber name="myUniqueNumber1" guid="guid" link-datafield="guid">
		<part type="constant" value="UP-"/>
		<part type="year" condition="true" format="yyyy"/>
		<part type="month" condition="true" format="mm"/>
		<part type="day" condition="true" format="dd"/>
		<part type="constant" value="-"/>
		<part type="number" format="###"/>

Create a new file with a standard text editor, and copy the code above into the file. Amend the values below as follows:

  • name

    Name of the relevant unique number (freely selectable, must be unique)

  • guid

    GUID of the relevant unique number (freely selectable, must be unique)

  • link-datafield

    GUID of the string data field where the unique number will be generated

  • <part .../>

    Definition of a part of the unique number

Enter a name for your unique number in "name".

A GUID for the unique number can be generated easily via the menu "Extras / Create GUID".

To identify the GUID of the data field, where the unique number will be generated, select the data field in the application structure and press the F4 key.

In the UniqueNumber.xml file, you can define as many unique numbers as you require. For additional unique number definitions, copy this section

<uniquenumber name=" myUniqueNumber1" guid="guid" link-datafield="guid">...

and replace the entries for "name", "guid", "link-datafield" and the "part types" with the corresponding new values.

In the "part-type" tags that follow, you can specify one part of the unique number in each tag. In our example here, the new number begins with "UP-", followed by the four-digit year, the month, the day, a hyphen and a three digit number that is automatically incremented. More information about this is available in the next chapter. Save the file with the name "UniqueNumber.xml" in the portal directory internal/cfg.

Now the portal service needs to be restarted. You can see the result in the browser below.

Part types

The individual parts of a unique number and their ordering are defined by the different "part type" elements.

part type: constant

<part type="constant" value="UP-"/<

Generates a fixed string value of your choice.

part type: number

<part type="number" format="###"/<

Generates an incremental number that increases by 1 each time. The length of the number part is controlled by the number of hash signs (#). format="###" thus creates a three-digit number part for the unique number in the syntax 001, 002, 003, etc. Once the highest possible number has been reached (here in the example 999), it is started again at 001.

part type: Time/Date

Each part of the date/time must be created individually. All of the required part types from year through month, day, hour, minute to second are available here.

<part type="year"/> 
<part type="month"/> 
<part type="day"/> 
<part type="hour"/> 
<part type="minute"/> 
<part type="second"/>

If a condition is used with the date/time part types (year, month, day, hour, minute and second), then the number part of the unique number will restart from 1 if one of the corresponding date/time parts change. When using such a condition, however, it is important to ensure that the date/time part types are placed in sequence from the highest (year) to the lowest (second).

<part type="year" condition="true"/> 
<part type="month" condition="true"/> 
<part type="day" condition="true"/>

The "format" option allows you to customize the format of the date/time parts.

<part type="year" format="yyyy"/> 
<part type="month" format="mm"/> 
<part type="day" format="dd"/>


The Unique Number counter can be reset to 1 with Groovy script - e.g. in a Groovy action in a process. The corresponding Groovy script reads:

import de.uplanet.lucy.server.businesslogic.util.UniqueNumberGenerator

Replace "BB575B3BAC1C880D69308647B283BFADC052C089" with the GUID of your Unique Number Generator data field.