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

Recommended Reading:

Managing Your Business Through Uncertain Times Using Dynamics 365 Finance and Operations

  Dynamics 365 Finance and Operations (F&O) can help you make informed decisions on how to move your business forward. […]

Read the Article
5.13.22 Power Platform

Using Power BI Object Level Security

  The following article will demonstrate how to use Power BI Object Level Security to disable column data based on […]

Read the Article
5.12.22 Dynamics CRM

How to Use the Stoneridge Support Portal

Stoneridge Software’s support portal is an intuitive and useful function that makes it easy for you to access resources to […]

Read the Article
5.6.22 Dynamics GP

Dynamics GP Transaction Removal: Purchase Orders

  Are you having performance issues with Purchase Orders?  Do you find that there are old Purchase Orders on your […]

Read the Article
5.5.22 Dynamics GP

The Real Story about the Long-Term Future of Dynamics GP Support

I’ve seen a number of people put forward comment that Dynamics GP is going away and you have to get […]

Read the Article

New Features in Dynamics 365 Business Central 2022 Wave 1 Release – Financial Enhancements

The Dynamics 365 Businses Central 2022 Wave 1 Release has a lot of new and exciting features to help your […]

Read the Article
4.29.22 Dynamics GP

Dynamics GP Transaction Removals: Bank Reconciliation

  This is part 2 of a 3 part series on Dynamics GP Transaction Removals. These quick tips will hopefully […]

Read the Article
4.28.22 Dynamics GP

Uncommonly Used Features – Integrate Purchasing and Payables to Fixed Assets in Dynamics GP

Being able to integrate Purchasing and Payables to Fixed Assets in Dynamics GP will help you create visibility for your […]

Read the Article

New Features in the Dynamics 365 Business Central 2022 Wave 1 Release – Ease-of-Use Features

  There are many new features in the Dynamics 365 Business Central 2022 Wave 1 Release to get excited about! […]

Read the Article

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!

X