Initializing Number Sequences from Code in AX

By Mark Nelson | September 16, 2015

While writing unit tests for a feature I was developing in AX I decided to use the SysTestSuiteCompIsolateClassWithTts test suite class. This class isolates the changes made by your test by creating a new, temporary company, running everything within a transaction, and aborting the transaction when the suite finishes. Unfortunately it does not setup any number sequences for you; and because a new company is created every time the tests are run, you cannot run the Number Sequence wizard like you would normally do. To fix this you must initialize the number sequences yourself from within your test's transaction.

Initializing a Number Sequence

Number Sequences are initialized for a specific EDT and scope.  This method initializes the number sequences for the InventDimId, SalesId, and ItemId extended data types.  Data for each of these types is added to the temporary TmpNumberSeqCreate table by a helper method before being processed by the NumberSequenceTable::createFromTmpNumberSeqCreate method.

private void setupNumberSequences(NumberSeqScope _numberSeqScope = NumberSeqScopeFactory::createDataAreaScope())
{
    TmpNumberSeqCreate tmpNumberSeqCreate;

    NumberSequenceTable numberSequenceTable;
    NumberSequenceReference numberSequenceReference;
    NumberSequenceDatatype numberSequenceDatatype;

    ttsbegin;

    // This is an example.  Add a call for each EDT.
    this.setupNumberSequence(extendedtypenum(InventDimId), tmpNumberSeqCreate, _numberSeqScope);
    this.setupNumberSequence(extendedtypenum(SalesId), tmpNumberSeqCreate, _numberSeqScope);
    this.setupNumberSequence(extendedtypenum(ItemId), tmpNumberSeqCreate, _numberSeqScope);

    while select tmpNumberSeqCreate
        where tmpNumberSeqCreate.NumberSequence != ''
    {
        // Creating number sequence %1
        info(strfmt("@SYS50682", tmpNumberSeqCreate.NumberSequence));
        NumberSequenceTable::createFromTmpNumberSeqCreate(tmpNumberSeqCreate);
    }

    ttscommit;
}

The setupNumberSequence helper method is responsible for initializing the temp table with information about the extended data type.

private void setupNumberSequence(extendedTypeId _extendedTypeId, TmpNumberSeqCreate _tmpNumberSeqCreate, NumberSeqScope _numberSeqScope = NumberSeqScopeFactory::createDataAreaScope())
{
    RefRecId  legalEntityRecID;
    NumberSequenceDatatype datatype;
    boolean initOK;

    datatype = NumberSequenceDatatype::findByDatatypeId(_extendedTypeId);

    initOK = TmpNumberSeqCreate::initForDatatype(_tmpNumberSeqCreate, datatype, _numberSeqScope, true);
    if (initOK == false)
    {
        throw error(strFmt("Failed to init Number Sequence %1", _tmpNumberSeqCreate.NumberSequence));
    }

    _tmpNumberSeqCreate.insert();
}

Alternatively, you could setup all number sequences by populating the temp table with the TmpNumberSeqCreate::buildWizardList method. This is usually overkill for targeted unit tests because the time spent initializing the number sequences is usually greater than the time spent executing the tests.  This code can be called from the setUpTestCase method of your test suite.

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!