Tips & Tricks - Year-Based Numbering

This post shows how to generate a transaction number from a transaction date and a transaction counter—a number that starts at 1 each year, is automatically incremented, and includes the year in the number. Prior experience with Groovy and a good knowledge of Intrexx are beneficial for this post. You can download the sample application with the process here and import it as usual. Enable the included process so you can test our example.

In the sample application, three input fields with corresponding data fields have been created on the input page:

  • Transaction Date (Control Type: Date)

  • Transaction Counter (Control Type: Integer)

  • Transaction Number (Control Type: Text)

To generate the transaction number, a process must respond to the insertion of a data record into the data group.

The following Groovy action executes the following script:

            // Import required classes
import java.util.Calendar
import java.text.SimpleDateFormat

// Initialize data connection to data base
def conn = g_dbConnections.systemConnection

// Get date of process
// Enter the GUID of the data field "Operationdate" here
def dtOperationdate = g_record["0158FC7B4355CA2D10A1621A9A7CAA8F53ECB2AC"].value

// Determine time zone of the logged in user
def tz = g_session.user.timeZone

// Determine year numbers from operation date
// Four digit year
def strYear = String.format('%tY', dtOperationdate)
// Two digit year
def strYearShort = String.format('%ty', dtOperationdate)

// Create start date for meter reading determination
// (01.01. start of operation year)
def calStart = Calendar.getInstance(tz)
calStart.setTime(dtOperationdate)
calStart.set(Calendar.MONTH, Calendar.JANUARY)
calStart.set(Calendar.DATE, 1)
calStart.set(Calendar.HOUR, 0)
calStart.set(Calendar.MINUTE, 0)
calStart.set(Calendar.SECOND, 0)
calStart.set(Calendar.MILLISECOND, 0)

// Create end date for meter reading determination
//(31.12.end of operation year)
def calEnd = Calendar.getInstance(tz)
calEnd.setTime(dtOperationdate)
calEnd.set(Calendar.MONTH, Calendar.JANUARY)
calEnd.set(Calendar.YEAR, strYear.toInteger() + 1)
calEnd.set(Calendar.DATE, 1)
calEnd.set(Calendar.HOUR, 0)
calEnd.set(Calendar.MINUTE, 0)
calEnd.set(Calendar.SECOND, 0)
calEnd.set(Calendar.MILLISECOND, 0)

// Determine the last counter reading in the transaction year and
// create the new transaction number with +1
// Enter the GUID of the data group "transaction" here
def intCounter = g_dbQuery.executeAndGetScalarIntValue(conn, "SELECT MAX(L_OPERATIONCOUNTER)+1 FROM DATAGROUP('27B3340528694E79C45E3A7F693F287DE0ABC758') WHERE DT_OPERATIONDATE >= ? AND DT_OPERATIONDATE < ?", 1){
	setTimestamp(1, calStart.time)
	setTimestamp(2, calEnd.time)
}

// Formatting the operation number
def strVorgangsnummer = strYearShort + "-" + intCounter.toString().padLeft(4, "0")

// Put results into processing context
g_sharedState.put("Operationnumber", strOperationnumber)
g_sharedState.put("Operationcounter", intCounter)

        

The script determines the highest counter number for the transaction year and uses it to assign a number to the new record. To do this, the year from the date is extracted, and two comparison dates for that year are generated. To do this, define two calendar objects with the dates 01/01/<previous year> and 01/01/<previous year + 1>, respectively. The second date value essentially represents December 31; however, the comparison in the SQL statement must yield January 1. can be used with a time value of 00:00 and the operator "<" to include the last minute of the old year. The results of the calculations are placed in the processing context as variables. Replace if necessary. The following values if you want to use the Groovy script in other applications:

  • GUID of the "Transaction Date" data field (0158FC7B4355CA2D10A1621A9A7CAA8F53ECB2AC in the example)

  • GUID of the "Transaction" data group (27B3340528694E79C45E3A7F693F287DE0ABC758 in the example)

Also make sure the data field names in the database query are correct.

If you want the count to always start at a different value, you can set the fallback value to a different value when determining the new counter number. In the following example, the count starts at 1,000.

            // Enter the GUID of the "Process" data group here.
def intCounter = g_dbQuery.executeAndGetScalarIntValue(conn, "SELECT MAX(L_OPERATIONCOUNTER)+1 FROM DATAGROUP('27B3340528694E79C45E3A7F693F287DE0ABC758') WHERE DT_OPERATIONDATE>= ? AND DT_OPERATIONDATE < ?", 1000){

        

The data record is modified in the following data group action.

To do this, a user-defined system value of the "Processing Context" type is created in the field mapping for both the operation number and the operation counter, and is assigned to the corresponding data fields.