Wednesday, 28 November 2012



How to Generate Number Sequence in  MicroSoft Dynamics Ax /
Generate NumberSequence with X++ Code.



Step1:
 Create a new EDT  named as NoobId  should be string.

Step 2:
Suggest I have a table named NoobsTable and a field named NoobId, the extended data type for NoobId is NoobEDT.
I have a Form named  NoobsForm.
Step3:
 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     atatype related to NoobId.
    numRef.referenceHelp             =  literalStr("HelpText");
    numRef.wizardContinuous         = true;
    numRef.wizardManual             = NoYes::No;
    numRef.wizardAllowChangeDown    = NoYes::No;
    numRef.wizardAllowChangeUp      = NoYes::No;
    numRef.wizardHighest            = 99999999;
    numRef.sortField                = 1;
    this.create(numRef);

1. Create a new job with the following code and run it:
static void NumberSeqLoadAll(Args _args)
{
NumberSeqApplicationModule::loadAll();
}

Step4:

Then go to Table node and  NoobsTable, 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
      }
Exmaple:

server public static NumberSequenceReference  numRefHID()
{
    return NumberSeqReference::findReference(extendedTypeNum(HID));
}

Step5:
Ax2012 path:

Run the number sequence wizard by clicking on the Generate button in Organization administration | Common | Number sequences | Number sequences, and click on the Next button.

Ax2009 path:

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 

Step6:

 Go to NoobsForm -->classdeclaration

public class FormRun extends ObjectRun
{
    NumberSeqFormHandler    numberSeqFormHandler;
}

NoobsForm > >new Method

NumberSeqFormHandler numberSeqFormHandler()
{
if (!numberSeqFormHandler)
{
numberSeqFormHandler = NumberSeqFormHandler::newForm(
                                                     NoobsTable:: numRefHID ().NumberSequenceId,
                                                     element, NoobsForm.dataSource(),
                                                     fieldNum(NoobsTable, HID));

}
return numberSeqFormHandler;
}



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

public void create(boolean _append = false)
{
element.numberSeqFormHandler().formMethodDataSourceCreatePre();
super(_append);
element.numberSeqFormHandler().formMethodDataSourceCreate();
}

public void write()
{
super();
element.numberSeqFormHandler().formMethodDataSourceWrite();
}

public void delete()
{
element.numberSeqFormHandler().formMethodDataSourceDelete();
super();
}

Note: Just close once your Ax application and Open it.
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.

No comments:

Post a Comment