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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
Thanks, this is very useful.
I wish more people cared about unit tests.
Cheers.
Hello Marcel,
We couldn’t agree more. Unit tests are an important step in development.
Thank you,
Brandon
Hi Mr. Mark Nelson
This is a nice post to do the process to start the number secuence. Thanks for this!
In other way i want to ask to you if it’s possible.
What i need to register a number secuence into NUMBERSEQUENCEDATATYPE table? Because when i try to do a createReferenceMulti from NumberSeqApplicationModule i got a error like “the value XX it’s not located into a map” and the problem it’s this module don’t exist in this table
Best regards
Hello Eduardo,
After talking with Mark, we suggest that you take a look at the NumberSequenceTable table’s autoCreate method.
Thanks,
Brandon