MSBA-341 · Lesson 6 of 12 · 19 min

Number sequences for developers

▶ Video coming soon A video version of this lesson is on the way.

Inspection certificates needed their own identifiers. The foundries put their own certificate numbers on the paperwork, but those numbers are not unique across suppliers and some foundries reuse them every year. Thandeka wanted every certificate Cobalt Ridge recorded to carry an internal number that auditors could follow from receipt to inventory, following the same numbering convention as every other document in the project. That meant a new number sequence, and number sequences are one of the places where functional setup and developer work meet.

What a number sequence is

A number sequence generates identifiers for records such as purchase orders, vouchers and customer accounts. Administrators set up number sequence codes, each with a format, a range and properties such as whether the sequence is continuous. Each code is assigned to a reference, which is the slot in the application that needs numbers, such as Purchase order in the procurement parameters. The application asks for the next number from whatever code is assigned to the reference. Cobalt Ridge's convention, set in the foundations course, is that codes are named by legal entity and document, such as CRCA_PO, with formats like PO-CRCA-######.

Division of work between developers and administrators.
ConceptWho sets itExample
Reference (data type)Developer, in codeInspection certificate, based on EDT CRInspCertId
ScopeDeveloper chooses allowed scopes; administrator picks oneCompany
Number sequence codeAdministratorCRCA_INSP
Format and rangeAdministratorINS-CRCA-###### from 1 to 999999
Continuous or notAdministrator, within what the developer allowsNon-continuous with preallocation
Code that fetches numbersDeveloperNumberSeq::newGetNum(...)
Five boxes connected left to right by arrows: module class loadModule; reference EDT CRInspCertId; code CRCA_INSP with format INS-#####; parameters numRef method; NumberSeq::newGetNum. A note below says developers write the module and the numRef method, and administrators create the code and the format.
The path from a reference defined in code to a number on a record.

Defining a reference in a module

References are defined in number sequence module classes, which extend NumberSeqApplicationModule. Each module belongs to an area of the application, such as procurement, and its loadModule method creates a NumberSeqDatatype object for every reference the module offers. The datatype names the extended data type that will hold the number, gives a help text that appears in the setup forms, sets defaults for the wizard, and lists the scopes the reference supports, such as company. When the module runs, these definitions are written to the database so that administrators can see the reference and assign a code to it.

Because the certificate belongs to procurement, Desmond decided to add the reference to the standard purchase order module rather than create a new module. The standard module class is in Application Suite, so the reference is added with a Chain of Command wrapper on its loadModule method. The wrapper calls next so that all the standard references are still loaded, then creates the new datatype.

Adding the inspection certificate reference to the purchase order module. Check the method's exact access modifier in the reference metadata before wrapping it.
[ExtensionOf(classStr(NumberSeqModulePurchaseOrder))]
final class NumberSeqModulePurchaseOrder_CRInspection_Extension
{
    protected void loadModule()
    {
        next loadModule();

        NumberSeqDatatype datatype = NumberSeqDatatype::construct();
        datatype.parmDatatypeId(extendedTypeNum(CRInspCertId));
        datatype.parmReferenceHelp(literalStr("@CRCustom:InspCertNumberHelp"));
        datatype.parmWizardIsContinuous(false);
        datatype.parmWizardIsManual(NoYes::No);
        datatype.parmWizardHighest(999999);
        datatype.parmSortField(50);
        datatype.addParameterType(NumberSeqParameterType::DataArea, true, false);
        this.create(datatype);
    }
}

Prudence asked why the code used an EDT instead of a field. The number sequence framework identifies references by extended data type, which is one reason every document number in finance and operations apps has its own EDT. CRInspCertId was based on a string of 20 characters, long enough for the format, with the label Inspection certificate. The same EDT would be used for the field on the certificate table, which tied the two together.

Creating a new module

If a feature belongs to no existing area, a developer creates a new module class. The module needs a unique module value, which in practice means extending the NumberSeqModule enum, and it must register itself so the framework knows it exists. Registration is done by subscribing to a delegate on NumberSeqGlobal that builds the map of modules, and adding the new module class to that map. Desmond showed Prudence the pattern but did not use it; one reference in an existing module was simpler and put the setup where procurement administrators would look for it.

The parameters method

Code that needs a number does not mention the number sequence code. It asks for the reference, and by convention each parameters table has a static method for each of its references. The procurement parameters table is PurchParameters, so the extension added a static method to it through a class extension. The method finds the reference for the EDT in the current company. Any code that creates a certificate then calls that method and asks the number sequence framework for the next number.

A parameters method added through an extension, and the call that fetches the next number.
[ExtensionOf(tableStr(PurchParameters))]
final class PurchParameters_CRInspection_Extension
{
    public static NumberSequenceReference crNumRefInspCertId()
    {
        return NumberSeqReference::findReference(extendedTypeNum(CRInspCertId));
    }
}

// In the certificate service:
CRInspCertId certId = NumberSeq::newGetNum(PurchParameters::crNumRefInspCertId()).num();

Getting the reference onto the parameters form

Administrators assign codes to references on the Number sequences tab of the relevant parameters form, and on the Number sequences page itself. For the new reference to appear there, two things must be true. First, the reference must have been loaded into the database, which happens when the module definitions are regenerated, for example when an administrator runs the number sequence generation wizard after the deployment. Second, the parameters form must show references for its module, which it does automatically for references in the standard module. Prudence deployed the package, ran the wizard in her environment, and found Inspection certificate listed alongside Purchase order and Purchase requisition, ready for a code.

Continuous and non-continuous sequences

A continuous sequence guarantees there are no gaps. If a number is fetched and the record is not saved, the number is returned to the pool and reused. That guarantee has a cost: continuous sequences lock and track every number, which slows high-volume processes. A non-continuous sequence simply hands out the next number, and gaps can appear. For performance, non-continuous sequences can use preallocation, where the server reserves a block of numbers in memory and hands them out without going to the database each time.

Legal requirements decide which to use. In many jurisdictions, financial vouchers must be continuous, which is why Cobalt Ridge's vouchers are set up that way. Inspection certificates have no such rule, and receipts are posted in volume at the warehouse, so Desmond set the wizard default to non-continuous and Annelise configured CRCA_INSP with preallocation of ten numbers. If auditors later asked for no gaps, the administrator could change the code to continuous without a code change, because the reference allowed it.

Choosing between continuous and non-continuous sequences.
ContinuousNon-continuous
GapsNone; unused numbers are returned and reusedPossible when a fetched number is not used
PerformanceSlower; numbers are tracked in a listFaster; preallocation can reserve blocks
Typical useFinancial vouchers where law requires no gapsCertificates, journal batch numbers, most master data
Code differencesUse the form handler or mark numbers used or released when records are saved or abandonedSimple newGetNum is enough

Numbers on forms

When a user creates a record on a form and the number should appear immediately, standard forms use a number sequence form handler, an object of class NumberSeqFormHandler created by the form for the data source. The handler fetches a number when the user creates a record, shows it, and returns it to the pool if the user abandons the record, which matters for continuous sequences. For Cobalt Ridge's certificate form, Desmond added the form handler so that a buyer creating a certificate by hand would see the number straight away, while certificates created during receipt processing used newGetNum in the service class.

Manual sequences and scopes

Some references allow a manual setting, in which users type their own numbers. The datatype's wizard settings control whether manual is the default and whether it can be changed. Cobalt Ridge did not allow manual certificate numbers, because the point was a controlled internal identifier. Scopes control where a sequence applies: the certificate reference allowed only the company scope, so CRCA, CRUS and CRMX each had their own code, CRCA_INSP, CRUS_INSP and CRMX_INSP, and numbers never collided because the format included the entity.

Scopes and segments

The scope of a number sequence decides where its numbers are unique. Shared scope means one sequence for the whole installation; company scope means one per legal entity; some references support further scopes such as operating unit or fiscal calendar period, which is common for vouchers that restart numbering each period. The datatype's addParameterType calls in loadModule list the scopes a reference supports, and the administrator then picks one when creating the code. The scope's values can appear as segments in the format, which is why Cobalt Ridge's certificate numbers include the company identifier.

Troubleshooting number sequences

Number sequence problems show up as error messages at the moment a record is created, which makes them visible but sometimes confusing for users. The most common one is that no number sequence code has been assigned to the reference, usually because the setup step after deployment was missed in one company. Another is that a sequence has reached its largest value, which the administrator fixes by extending the range or changing the format. A third, with continuous sequences, is a growing list of numbers in use that were never released, typically from a process that fetched numbers without saving records. Finance and operations apps include a periodic cleanup for continuous number sequences, which administrators can schedule.

Prudence added a check to the certificate service that gave a clear, labelled message if the reference had no code assigned, naming the parameters page where an administrator could fix it. Desmond liked the idea and added it to the project's coding standards, because a clear message turns a support call into a five-minute fix.

Numbers in data imports

Data imports raise a design question: should records created through a data entity get numbers from the sequence, or should the import file supply them? Some standard entities can fetch numbers from the sequence when the number field is left empty, and some require the number in the file. For CRInspectionCertificateEntity, Desmond decided that migrated certificates from the old system would keep their old internal numbers, loaded from the file into a separate legacy field, and the entity would fetch a new number from the sequence when the CRInspCertId field was empty. That way, migrated and new certificates followed the same format, and auditors could still find the old number.

Number sequences and performance

Number sequences can become a bottleneck in high-volume processing. Every continuous number requires a database round trip and a record in the list of numbers in use, and many parallel batch tasks fetching from the same continuous sequence can wait on each other. Non-continuous sequences with preallocation avoid most of that cost. When Prudence later measured receipt posting for a large casting delivery in the developer environment, the certificate numbers added no noticeable time, which confirmed the choice of a non-continuous sequence.

Desmond ended the session with a reminder about documentation. Every number sequence reference added by the model went into the release notes with its EDT, the module it belonged to, the scopes it allowed and the recommended format, so that Annelise could set it up in each company without asking a developer.

Worked problem

Testing the new sequence

After deploying the extension, Prudence needs to show Desmond that certificate numbers work in all three companies and that an abandoned certificate on the form does not waste a number if the sequence is later made continuous.

Show solution
  1. Run the number sequence generation wizard in the developer environment and confirm that Inspection certificate appears with three proposed codes, one per company.
  2. Assign CRCA_INSP, CRUS_INSP and CRMX_INSP with the agreed formats and set the CRCA code to non-continuous with preallocation.
  3. Create certificates through the service in each company and check the formats.
  4. Change a test copy of the code to continuous, create a certificate on the form, close it without saving, and confirm the next certificate reuses the number.

Answer: The extension defines the reference; the administrator's setup decides the format and behaviour. Testing both continuous and non-continuous settings proves the code works with either.

Common mistakes

  • MistakeHard-coding a number sequence code in X++.

    InsteadAsk for the reference through a parameters method so administrators can assign any code.

  • MistakeForgetting to call next in the loadModule wrapper.

    InsteadCall next first so the standard references still load.

  • MistakeMaking every new sequence continuous by default.

    InsteadUse continuous only where rules require no gaps; it costs performance.

  • MistakeExpecting a new reference to appear before the definitions are regenerated.

    InsteadRun the generation step after deployment so the reference is written to the database.

Summary and key terms

  • References are defined by developers in number sequence module classes; codes, formats and continuity are set by administrators.
  • A new reference can be added to a standard module by wrapping its loadModule method and creating a NumberSeqDatatype.
  • Parameters tables expose a static numRef method per reference, and code fetches numbers with NumberSeq::newGetNum.
  • Continuous sequences avoid gaps at a performance cost; non-continuous sequences can use preallocation.
  • Forms use a number sequence form handler so numbers appear on create and are released if the record is abandoned.
Number sequence reference
The slot in the application, identified by an EDT, that needs numbers.
Number sequence code
The administrator's setup that defines format, range and continuity.
Continuous sequence
A sequence that never leaves gaps; unused numbers are reused.
Preallocation
Reserving a block of numbers in memory to speed up non-continuous sequences.
NumberSeqFormHandler
The class that manages numbers for records created on forms.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Why does the framework use an EDT to identify a reference?

    Show answer

    Each document number has its own EDT, which uniquely identifies the reference and is reused by the field that stores the number.

  2. Which scope did Cobalt Ridge choose for certificates and why?

    Show answer

    Company, so each legal entity has its own code and format.

  3. Can an administrator make CRCA_INSP continuous later without a code change?

    Show answer

    Yes, if the reference allows it; continuity is a property of the code.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz