Jun 6, 2010

How to create new number sequence though code in Ax.

To let any field to be generated sequentially you must do the following:

Suggest i have a table named NoobsTable and a field named NoobId, the extended data type for NoobId is NoobEDT
and i have a Form named NoobsForm

1) For example if the NoobsForm is located in Inventory managment Module you should find the class named NumberSeqReference_Inventory, then go to LoadModule method in that class and write the following at last:

numRef.dataTypeId = typeId2ExtendedTypeId(typeid(NoobEDT)); //Extended datatype related to NoobId.
numRef.referenceHelp = literalStr("HelpText"); // Your help text to be viewed in Parameters later.
numRef.wizardContinuous = true;
numRef.wizardManual = NoYes::No;
numRef.wizardAllowChangeDown = NoYes::No;
numRef.wizardAllowChangeUp = NoYes::No;
numRef.wizardHighest = 99999999;
numRef.sortField = 1;

this.create(numRef);

2)Then go to Table node and find InventParameters, go to methods node then add a new method and write the following:

server static NumberSequenceReference numRefNoobId()
{
return NumberSeqReference::findReference(typeId2ExtendedTypeId(typeid(NoobEDT))); //Extended datatype
}

3)Then go to Inventroy managment content pane --> Setup --> Parameters --> Number Sequences Tab
now here you will see the Extended Data type NoobEDT and an empty Sequence number code, right click the empty lookup and click Go to the main table Form.
Add a new number sequence code in the opened form and save it [for example]:
Number sequence code: NoobNumberSeq
Name: NoobNumberSeq
Smallest: 1
Largest: 99999999
Next: 1
Format: Noob_########
InUse: Checked

Return to previous Form "Parameters" and choose NoobNumberSeq that we have just created it from Number sequence code lookup to NoobEDT Reference

3)Last thing to do is Go to NoobsForm --> Datasources node --> NoobsTable --> Methods, and override method (create) and write the following:

public void create(boolean _append = false)
{
;
super(_append);

NoobsTable.NoobId = NumberSeq::newGetNum(InventParameters::numRefNoobId(),true).num(); //numRefNoobId() is a method created in step 2)
}

//End

Finally go to the form and create a new record and you will see your number is generated automatically and sequentially.

Note: go to NoobsTable and set NoobId field to( AllowEdit:No AllowEditOnCreate:No ) if you dont want anyone to edit the generated number.

Atrees

No comments: