Search code examples
axaptadynamics-ax-2012number-sequence

Number sequence for custom table in ax 2012


I have a custom utility which uses a custom table in AX 2012 AOT, I want to have an identity field for my table and someone tell me I can use number sequences for this, and can map a number sequence to my table so it can get a new unique id at the time of row insert, when I try to generate number sequence it asks me AREA and module information, since i want this for my custom table and utility which is working outside the dynamics ax 2012 using .net business connector, I am unable to figure out what to input the wizard.


Solution

  • You have probably seen the method of using NumberSeqModule enum and adding a custom module enum value to it. Then you would normally create a class that extends the NumberSeqApplicationModule class and then load the numbersequences linked to the data types using number sequence references.

    Though this is the 'best practice way', it is a bit of overkill for what you would want to do now. So here is what you can do:

    You could just overwrite the insert method of the table you are using. And the you could use the newGetNumFromCode constructor on the NumberSeq class.

    public static NumberSeq newGetNumFromCode(
        NumberSequenceCode  _numberSequenceCode,
        NumberSeqScope      _scope = NumberSeqScopeFactory::createDefaultScope(),
        boolean             _makeDecisionLater           = false,
        boolean             _dontThrowOnMissingRefSetUp  = false,
        SysLastValue        _nextNumSeq = null,
        boolean             _fillNextNumSeq = false)
    {
        return NumberSeq::newGetNumFromId(
                NumberSequenceTable::findByNaturalKey(_numberSequenceCode, _scope.getId()).RecId,
                _makeDecisionLater,
                _dontThrowOnMissingRefSetUp,
                _nextNumSeq,
                _fillNextNumSeq);
    }
    

    So now to use this, you just have to create a new numbersequence within Dynamics Ax (Organization administration | Number Sequences | Number Sequences) and remember the number sequence code.

    Then on the insert method of your table you can do the following: (the example is taken from the contact person table)

    if (!this.ContactPersonId)
    {
        this.ContactPersonId = NumberSeq::newGetNum(CompanyInfo::numRefContactPersonId()).num();
    }
    

    That should make sure that when a record is inserted, you will also fill the ID.