Reset auto number

I have extended the current auto-number generation with some additional element like adding company name or static prefix etc. which is working perfectly.

Now I want to reset the auto-number to specific number that we want to for which gave done the as follows:

My number generation program in a service is as follows:

    public long getNextDocNumberWithPrefix(String docName, String companyCode, String plantCode, @Nullable FinancialYear financialYear) {

        SEQ_PREFIX = "";
        SEQ_PREFIX2 = "";
        SEQ_MID = "";
        TOTAL_DIGIT = 0;
        EXCLUDE_PREFIX = false;

        String year="";
        if(financialYear ==null){
            long coCo = Long.valueOf(companyCode);
            financialYear = inteaccUtilService.getFinancialYearCurrent(inteaccUtilService.getCompanyFinancialYearCurrent(coCo));
        }else {
           financialYear = inteaccEntityDataService.getEntityById(FinancialYear.class, financialYear.getUuid(), "financialYearFrom-view");
        }
        year = financialYear.getYearFrom().toString();
        UniqueNumbersAPI numbersBean = AppBeans.get(UniqueNumbersAPI.NAME);
        SEQ_PREFIX = getPrefix(docName, companyCode, plantCode, year);
        SEQ_PREFIX2 = SEQ_PREFIX;

        if(SEQ_PREFIX.equalsIgnoreCase("")){
            SEQ_PREFIX = docName;
        }

        Long number = numbersBean.getNextNumber(SEQ_PREFIX);

        if(SEQ_PREFIX2.equalsIgnoreCase(""))
            SEQ_PREFIX="";

        //generate number with fixed digits
        int n1 = SEQ_PREFIX.length();
        int n3 = number.toString().length();
        int n2 = TOTAL_DIGIT-n1-n3;
        for(int i=0; i<n2;i++){
            SEQ_MID = SEQ_MID+"0";
        }

        long seqNumber = new Long(0);
        if(EXCLUDE_PREFIX==true){
            seqNumber = number;
        }else {
            seqNumber = Long.valueOf(SEQ_PREFIX.concat(SEQ_MID).concat(number.toString()));
        }

        return seqNumber;
    }

The structure of Entity “NumberGenerationRule” that is used to maintain different unique number generation:

    create table ERP_NUMBER_GENERATION_RULE (
    ID uuid,
    VERSION integer not null,
    CREATE_TS timestamp,
    CREATED_BY varchar(50),
    UPDATE_TS timestamp,
    UPDATED_BY varchar(50),
    DELETE_TS timestamp,
    DELETED_BY varchar(50),
    --
    NAME varchar(255),
    DOC_NUMBER_IDENTIFIER bigint,
    NUMBER_PREFIX1 varchar(50),
    NUMBER_PREFIX2 varchar(50),
    NUMBER_PREFIX3 varchar(50),
    TOTAL_NUMBER_OF_DIGIT integer,
    EXCLUDE_PREFIX_KEEP_UNIQUENESS boolean,
    MANUAL_NUMBER_GENERATION boolean,
    COMPANY_GROUP_ID uuid,
    RESET_AUTO_NUMBER boolean,
    AUTO_NUMBER_NEXT_NEW bigint,
    --
    primary key (ID)
)

NumberGenerationEntityListener

      void updateAutoNumber(NumberGenerationRule entity){
        if(entity.getResetAutoNumber()==true){
            uniqueNumbersAPI.setCurrentNumber(entity.getName(),entity.getAutoNumberNextNew());
        }
    }

Now when I reset, it reset works but the number generated is not changed.
What could be the reason?

Hi Mortoza,
What database and what framework version are you using?

Hi Konstantin
I am using the latest version of the platform (6.10.5) and Postgres database.
regards
Mortoza

Try to use the app-core.cuba:type=UniqueNumbers JMX bean to set the current number. If it works, most probably your code uniqueNumbersAPI.setCurrentNumber() just isn’t invoked.

thank you Konstantin

Hi Konstantin
Sorry for coming back with feedback late.

I have reset the auto-number programmatically within my application without setting it from the JMX console that I see in JMX Console as follows:

31%20AM

That means the API is have used in my application worked correctly but the auto-number generated is still not changed i.e. old auto-number sequence remains unchanged. Thanks for helping to fix this!

Realized that, since the auto-number has prefix, I have to use the same prefix to the auto-number to reset, this was missing.