Skip to main content

[D365/AX7] How to: Create and Use number sequence

[D365/AX7] How to: Create number sequence

Posted on Updated on
Number sequences are used to generate readable, unique identifiers for master data records and transaction records that require identifiers. A master data record or transaction record that requires an identifier is referred to as a reference.
On this example, we are going to create a new number sequence for customer group.

Create a new class that extends the NumberSeqModuleCustomer class and then create the method loadModule_Extension and add the following code:
class NumberSeqModuleCustomer_CFS extends NumberSeqModuleCustomer
{
        public void loadModule_Extension()
        {
                NumberSeqDatatype datatype = NumberSeqDatatype::construct();

                datatype.parmDatatypeId(extendedTypeNum(CustGroupId));
                datatype.parmReferenceHelp("Customer group ID");
                datatype.parmWizardIsContinuous(false);
                datatype.parmWizardIsManual(NoYes::No);
                datatype.parmWizardIsChangeDownAllowed(NoYes::Yes);
                datatype.parmWizardIsChangeUpAllowed(NoYes::Yes);
                datatype.parmWizardHighest(999);
                datatype.parmSortField(20);
                datatype.addParameterType(NumberSeqParameterType::DataArea, true, false);

                this.create(datatype);
        }
}
Important: As we can see on Application Explorer, NumberSeqModuleCustomer belongs to Application Suite model, make sure the model which you are using for development extends this model.Untitled picture
Like in Dynamics AX 2012, we still have to initialize the references manually by creating a Job (Runnable class) with the following code:
class loadNumSeq_CFS
{
        public static void main(Args _args)
        {
                NumberSeqModuleCustomer_CFS numberSeqMod = new NumberSeqModuleCustomer_CFS();
                numberSeqMod.loadModule_Extension();
        }

}
Before proceeding build the solution and run it.
On Dynamics 365, go to Organization administration/Common/Number sequence:Untitled picture
Click on Generate button:
Untitled picture
It will open Number Sequences Wizard, click on Next button:
Untitled picture
Note the number sequence codes and click on the Next button:
Untitled picture
On the last page, click on the Finish button to complete the setup:
Untitled picture
The last thing we should create is a method to consume our newly created Number Sequence, we are going to create a new extension class of CustParameters table and call it CustParameters_Extension.
[ExtensionOf(tableStr(CustParameters))]
final class CustParameters_Extension
{
        client server static NumberSequenceReference numRefCustGroupId()
        {
                return NumberSeqReference::findReference(extendedTypeNum(CustGroupId));
        }

}
And that’s all the steps. Soon I will be posting how to use our Number Sequence.


[D365/AX7] How to: Use number sequence on forms

Posted on Updated on
Continuing from my post How to: Create number sequence, today I show how to use number sequence on forms.
On AX 2012 we have a class called NumberSeqFormHandler which is used to simplify the usage of record numbering on forms. This is still valid for Dynamics 365 for Finance and Operations.

First of all, we are going to create a new extension class for CustGroup form and add the class NumberSeqFormHandler to its class declaration.
[ExtensionOf(formStr(CustGroup))]
final class CustGroup_Extension
{
    public NumberSeqFormHandler numberSeqFormHandler;
}
Next we create a new method called numberSeqFormHandler() in the same class:
public NumberSeqFormHandler numberSeqFormHandler()
{
 if(!numberSeqFormHandler)
 {
 numberSeqFormHandler = NumberSeqFormHandler::newForm(
  CustParameters::numRefCustGroupId().NumberSequenceId, this, this.CustGroup_ds, fieldNum(CustGroup, CustGroup));
 }
 return numberSeqFormHandler;
}
This method instantiates the object if it has not been instantiated yet and returns it.
Now, we are going to create a method for each NumberSeqFormHandler’s methods, each one correspond to a form event.
2018-01-09 23_50_56-
To create an Event Handler go to CustGroup form and go to form’s datasource, expand Events node, right click on desired event and select Copy event handler method and paste it on our class.
2018-01-09 22_36_31-D365_NumberSequence - Microsoft Visual Studio (Administrator)
This is the result:
[ExtensionOf(formStr(CustGroup))]
final class CustGroup_Extension
{
    public NumberSeqFormHandler numberSeqFormHandler;

    public NumberSeqFormHandler numberSeqFormHandler()
    {
        if(!numberSeqFormHandler)
        {
          numberSeqFormHandler = NumberSeqFormHandler::newForm(
              CustParameters::numRefCustGroupId().NumberSequenceId, this, this.CustGroup_ds, fieldNum(CustGroup, CustGroup));
        }
        return numberSeqFormHandler;
   }

   [FormDataSourceEventHandler(formDataSourceStr(CustGroup, CustGroup), FormDataSourceEventType::Creating)]
   public void CustGroup_OnCreating(FormDataSource sender, FormDataSourceEventArgs e)
   {
   }

}
Now add the following code to our method:
this.numberSeqFormHandler().formMethodDataSourceCreatePre();
Now, repeat the above steps for the events OnCreated, OnDeleting, OnWritten, OnValidateWrite and OnClosing. The closing event belongs to form Event, not form datasource like the others.
At the end, this is the whole code for our class.
[ExtensionOf(formStr(CustGroup))]
final class CustGroup_Extension
{
    public NumberSeqFormHandler numberSeqFormHandler;

    public NumberSeqFormHandler numberSeqFormHandler()
    {
        if(!numberSeqFormHandler)
        {
            numberSeqFormHandler = NumberSeqFormHandler::newForm(
                CustParameters::numRefCustGroupId().NumberSequenceId, this, this.CustGroup_ds, fieldNum(CustGroup, CustGroup));
        }
        return numberSeqFormHandler;
    }

    [FormDataSourceEventHandler(formDataSourceStr(CustGroup, CustGroup), FormDataSourceEventType::Creating)]
    public void CustGroup_OnCreating(FormDataSource sender, FormDataSourceEventArgs e)
    {
        this.numberSeqFormHandler().formMethodDataSourceCreatePre();
    }

    [FormDataSourceEventHandler(formDataSourceStr(CustGroup, CustGroup), FormDataSourceEventType::Created)]
    public void CustGroup_OnCreated(FormDataSource sender, FormDataSourceEventArgs e)
    {
        this.numberSeqFormHandler().formMethodDataSourceCreate();
    }

    [FormDataSourceEventHandler(formDataSourceStr(CustGroup, CustGroup), FormDataSourceEventType::Deleting)]
    public void CustGroup_OnDeleting(FormDataSource sender, FormDataSourceEventArgs e)
    {
        this.numberSeqFormHandler().formMethodDataSourceDelete();
    }

    [FormDataSourceEventHandler(formDataSourceStr(CustGroup, CustGroup), FormDataSourceEventType::Written)]
    public void CustGroup_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)
    {
        this.numberSeqFormHandler().formMethodDataSourceWrite();
    }

    [FormDataSourceEventHandler(formDataSourceStr(CustGroup, CustGroup), FormDataSourceEventType::ValidatedWrite)]
    public void CustGroup_OnValidatedWrite(FormDataSource sender, FormDataSourceEventArgs e)
    {
        boolean ret = true;
        ret = this.numberSeqFormHandler().formMethodDataSourceValidateWrite();
    }

    [FormEventHandler(formStr(CustGroup), FormEventType::Closing)]
    public void CustGroup_OnClosing(xFormRun sender, FormEventArgs e)
    {
        numberSeqFormHandler.formMethodClose();
    }

}
Now build the code, open Dynamics and go to Accounts Receivable/Setup/Account Groups. Create a new record and check if the Customer group ID was automatically filled.
2018-01-14 22_35_11-Customer groups -- Finance and Operations - Internet Explorer

Comments

Popular posts from this blog

Number sequence with existing module and new module

Number sequence customization in Dynamics 365 Create a number sequence in a standard module Step 1: Create new EDT Step 2: Add a new code block to the loadModule in the NumberSeqModuleXXX class Step 3: Add a new static method to the parameters table of the module to get the number sequence reference Using Extension [ExtensionOf(classStr(NumberSeqModuleCustomer))] final class NumberSeqModuleCustTest_Extension { protected void loadModule() { NumberSeqDatatype datatype = NumberSeqDatatype::construct(); next loadModule(); datatype.parmDatatypeId(extendedTypeNum(TestId)); datatype.parmReferenceHelp(literalStr(‘Test ID’)); datatype.parmWizardIsContinuous(false); datatype.parmWizardIsManual(NoYes::No); datatype.parmWizardIsChangeDownAllowed(NoYes::No); datatype.parmWizardIsChangeUpAllowed(NoYes::No); datatype.parmSortField(1); datatype.addParameterType(NumberSeqParameterType::DataArea, true, false); this.create(datatype); } } [ExtensionOf(tableStr(CustParameters...

Batch Multi threading D365FO AX7

Batch Multi threading D365FO AX7 Normally, when we schedule our RunBaseBatch class as a batch job, it will create single task. Which will responsible to execute complete process in single thread. But, As you know like AX-2009 and AX-2012. In D365FO We can add multiple AOS server in single batch group and they can process the multiple task simultaneously.  In this demo I will show, How to create multi threading Batch job. following are the steps Step-1 Create a table for demo purpose in our demo we keep table name is  SLD_DemoTable . Step-2 create a class with name  SLD_DemoBusinessLogic  and write your business logic in this class. In demo I write only insert records in table and show you guys how it works. Reference Code class   SLD_DemoBusinessLogic {      public   void  processInit( VendTable  _vend)     {          SLD_DemoTable  ...