Unique numbers API problem


I have this code which is working fine for generating auto increase “order field”.
Now the question is how can I don’t make it increase the number next time, in case user dont save the record.
I am using uniqueNumbersService for auto increment but the limitation of it is , it wont setCurrentNumber.
For that I need to use uniqueNumbersAPI which is accessible only from the core module.
May be I can create a custom service and use it in controller but is it recommended?
Please help.

private UniqueNumbersService unService;

protected WebPickerField typeIdLookupField;

public void init(Map<String, Object> params) {
      this.typeIdLookupField.addValueChangeListener(e -> {
           CorpRTSUndIns corpRTSUndIns = getItem();
           corpRTSUndIns.setOrder((int) unService.getNextNumber(corpRTSUndIns.getTypeId().getRestictionType().replaceAll(" ", "").replaceAll("/", "")));


You do not want to create the unique numbers in the client tier. This is an example of how I handle unique numbers. In my case, I always set the number property to TBD (To Be Determined) and set the number when all client side validations have been performed and the record is being inserted.

In this example, I inherit from a Company and create a unique number in the format for example CU1803001 where the number represents a customer created in the third month of 2018 and is first. The number resets to 1 every month.

First create a listener:


Here is the Groovy code for the listener:

package com.pfc.planning.listener

import com.haulmont.cuba.core.app.UniqueNumbersAPI
import org.springframework.stereotype.Component
import com.haulmont.cuba.core.listener.BeforeInsertEntityListener
import com.haulmont.cuba.core.EntityManager
import com.pfc.planning.crm.Company
import com.pfc.planning.crm.Customer
import com.pfc.planning.project.Installer
import com.pfc.planning.purchasing.Vendor

import javax.inject.Inject
import java.text.SimpleDateFormat

public class CompanyEntityListener implements BeforeInsertEntityListener<Company> {
    private UniqueNumbersAPI uniqueNumbers

    public void onBeforeInsert(Company entity, EntityManager entityManager) {
        def prefix = "CO"
        if(entity.number == null || entity.number == "TBD") {
            if(entity instanceof  Customer)
                prefix = "CU"
            else if(entity instanceof  Vendor)
                prefix = "VE"
            else if(entity instanceof  Installer)
                prefix = "IN"

            def date = new SimpleDateFormat("yyMM").format(new Date())
            def prefixDate = String.format("%s%s", prefix, date)
            def number = uniqueNumbers.getNextNumber(String.format("%s.%s", entity.getClass().name, date))
            entity.number = String.format("%s%03d", prefixDate, number)

Hope this helps!



1 Like


Thank you for your suggestion. I tried similar approach but its not working in my case.


package com.sompo.intl.legal.loa.listener;

import javax.inject.Inject;

import org.springframework.stereotype.Component;
import com.haulmont.cuba.core.listener.BeforeInsertEntityListener;
import com.haulmont.cuba.core.EntityManager;
import com.haulmont.cuba.core.app.UniqueNumbersAPI;
import com.sompo.intl.legal.loa.entity.CorpRTSUndIns;

public class CorpRTSUndInsEntityListener implements BeforeInsertEntityListener<CorpRTSUndIns> {
    private UniqueNumbersAPI uniqueNumbers;

    public void onBeforeInsert(CorpRTSUndIns entity, EntityManager entityManager) {
    	if(entity.getOrder()== null) {
	           entity.setOrder((int) uniqueNumbers.getNextNumber(entity.getTypeId().getRestictionType().replaceAll(" ", "").replaceAll("/", "")));


Are you using an IDE? If so, set a breakpoint and see if you are even getting into the class on the insert. Also, make sure that you have restarted your app, as I have had issues where event listeners don’t register correctly on creation.

1 Like

Yes I am using Eclipse IDE, I tried with debug too, its not going to EntityListener Class at all.
I will check restarting app as well in a while.

Also, check your Entity and make sure that the @Listeners annotation was added properly to the top of the Entity.

Well above code was auto generated while creating listener , I just added that condition.
still need to add @Listeners annotation?

The @Listeners should be added by the generator when you create the listener. What do you mean you added the condition?

Well I dont know where you are suggesting to add @Listener annotation, but this is working great as expected after the app restart :slight_smile:
Thank you so much guys @vsposato5370 @IanE . You saved my day. :slight_smile:

Great news! For later reference, the @Listeners goes at the top of the entity that is being listened to. But if it is working, then it is probably there.

1 Like

@IanE Nice Idea to add Listener explicitly
@vsposato5370 Thank you for suggesting app restart.