For this demo, I will create New module along with NumberSequence, about creating Number Sequence without module you also use same steps just leave some steps base on Design picture above.
1. ETD
Create ETD ContosoId extends num datatype.
2. Table
Create Contoso Table with ContosoId field.
3. Enum
Create a new enum value Contoso in BaseEnum NumberSeqModule.
This value will be used to link number sequence to the module and to restrict displayed number sequence by module in Form.
4. NumberSeqModule Class
Create NumberSeqModuleXXX class
Create a new NumberSeqModuleXXX class, such as NumberSeqModuleContoso, which extends the NumberSeqApplicationModule class. The sample code for creating this class is as follows:
publicclassNumberSeqModuleContosoextendsNumberSeqApplicationModule{}protectedvoidloadModule(){NumberSeqDatatypedatatype=NumberSeqDatatype::construct();;/* Contoso Id */datatype.parmDatatypeId(extendedtypenum(ContosoId));datatype.parmReferenceHelp("ContosoId");datatype.parmWizardIsContinuous(true);datatype.parmWizardIsManual(NoYes::No);datatype.parmWizardIsChangeDownAllowed(NoYes::No);datatype.parmWizardIsChangeUpAllowed(NoYes::No);datatype.parmWizardHighest(999999);datatype.addParameterType(NumberSeqParameterType::DataArea,true,false);this.create(datatype);}publicNumberSeqModulenumberSeqModule(){;returnNumberSeqModule::Contoso;}
Use of the DataArea segment in Step 4 to describe the default segment for the extended data types used for ContosoId.
Note In Microsoft Dynamics AX 2009, number sequence references could be initialized by restarting the Application Object Server (AOS). In Microsoft Dynamics AX 2012, the initialization of references to populate the NumberSequenceDatatype and NumberSequenceParameterType tables has moved to the initialization checklist. To initialize the newly created references, run a job that executes the LoadModule method likes below.
1
2
3
4
5
6
7
staticvoidloadNumSeq(Args_args){//define the class variableNumberSeqModuleContososeqMod=newNumberSeqModuleContoso();//load the number sequences that were not generatedseqMod.load();}
You can also reinitialize all references by running a job that executes the LoadAll method in the NumberSequenceModuleSetup class. However, for reinitializing all references, you must ensure that there are no existing number sequences already defined in the system.
Then run the wizard in Organization Administration > CommonForms > Numbersequences > Numbersequences > Generate > run the wizard.
5. Parameters Table and Form
Create a Number sequences page in the parameters form of the new module.
You need to Create ContosoParameters Table along with form, See existing forms such as CustParameters or LedgerParameters for examples of the implementation.
These forms are using DetailsFormMaster form parten as Best Practice for Setup form.
5.1. Create ContosoParameters Table
Add field key, Extends from ParametersKey
Properties on Field key Visible = false, AllowEdit = false, AllowEditOnCreate = false
Create index name Key with AllowDuplicate = No.
Set table properties.
TableContent = Default data
ConfigurationKey
CacheLookup = Found
TableGroup = Parameter
PrimaryKey = Key
ClusterKey = Key
The sample code for creating method this table as below:
voiddelete(){throwerror("@SYS23721");}publicvoidinitValue(){;super();// Key is set to mandatory so set it to 1this.Key=1;}staticContosoParametersfind(boolean_forupdate=false){ContosoParametersparameter;if(_forupdate){parameter.selectForUpdate(_forupdate);}selectfirstonlyRecIdfromparameterwhereparameter.Key==1;if(!parameter&&!parameter.isTmp()){Company::createParameter(parameter);}returnparameter;}clientserverstaticNumberSeqModulenumberSeqModule(){;returnNumberSeqModule::Contoso;}publicserverstaticNumberSequenceReferencenumRefContosoId(){;NumberSeqScopeFactory::CreateDataAreaScope(curext());returnNumberSeqReference::findReference(extendedtypenum(ContosoId));}
5.2. Create ContosoParameters form
Note This form can only be used for references that have a scope of DataArea. The administration forms described in the Setup and Administration of number sequences section can be used for references that have any scope. These forms can be found in Organization Administration > Common > Number Sequences
The data source of Parameters form likes picture below, you can also refer to CustParameters form for design.
Optional method – in case you don’t want to expose Number sequence on Form Level
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
publicvoidinitValue(){;super();// Initialise the title idthis.setContosoId();}publicvoidinsert(){;if(!this.Id){this.setContosoId();}super();}
From now on you can create new record in Contoso Table with number sequence.
7. How to use on Form
How to use on form level (In case you don’t want to expose NS in Table Level)
In the class declaration of the form that will be accessing data, add a variable declaration for the number sequence handler. The following example shows the variable definition for a number sequence handler.
Add create, delete, and write methods to the data source of the table that contains the field for which the number sequence is being used. The following code examples show these methods that are added to the data source for the Contoso table to support the number sequence for the ContosoId field.
publicvoidcreate(boolean_append=false){//before create, (ensure the number seuence has not run out of numbers)element.numSeqFormHandlerContosoId().formMethodDataSourceCreatePre();// start: inherited table code// we need to let the create happen so the user can// choose the typesuper(_append);//number sequence, create action, (get next number)element.numSeqFormHandlerContosoId().formMethodDataSourceCreate();}publicvoiddelete(){//release the number sequence value.element.numSeqFormHandlerContosoId().formMethodDataSourceDelete();//delete the recordsuper();}publicvoidwrite(){super();element.numSeqFormHandlerContosoId().formMethodDataSourceWrite();}publicbooleanvalidateWrite(){booleanret;ret=super();ret=element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret)&&ret;if(ret){Contoso.validateWrite();}returnret;}LinkActive()publicvoidlinkActive(){;element.numberSeqFormHandler().formMethodDataSourceLinkActive();super();}
8. Optional method
Continuous sequence
To avoid having gaps for continuous sequence you should add this code to the delete of the table.
staticvoidMax_numberSeqRefCustAccount(Args_args){NumberSequenceReferencenumberSeqRefCustAccount=CustParameters::numRefCustAccount();NumberSeqnumSeqCustAccount;try{//Use the TTS level to decide whether sequence is consumed or not.ttsBegin;if(numberSeqRefCustAccount){numSeqCustAccount=NumberSeq::newGetNum(numberSeqRefCustAccount);if(numSeqCustAccount){info(numSeqCustAccount.num());}}ttsCommit;}catch(Exception::Error){info("Caught 'Exception::Error'.");}}