Translate

Monday, August 1, 2011

How to Use Number Sequence Engine Efficiently with Dynamics AX Forms

If you wanted to write a an X++ code to generate a number sequence and assign it to a field, you might use the following X ++ statement.

yourTableBuffer.Field = NumberSeq::newGetNum(NumberSequenceReference::find
(TypeID2ExtendedTypeId(TypeId(YourExtendedDataType)))).num();

And for the continuous number sequence (notice the “true” parameter):

yourTableBuffer.Field = NumberSeq::newGetNum(NumberSequenceReference::find
(TypeID2ExtendedTypeId(TypeId(YourExtendedDataType))), true).num();
But, what you could do if you have that field in form and you want to generate a number sequence while the user creates a new record is the class NumberSeqFormHandler.
NumberSeqFormHandler is a great class that doesn’t only take care of generating a new number sequence for your field… but also it takes care of removing or inserting that number in the Number Sequence List table when the number is not used (in case that the record hasn’t been inserted although the number has been already generated).
To use the NumberSeqFormHandler class, you have to declare a NumberSeqFormHandler object in the class declaration. Also you have to create a method at the Form level like the following:
//Form level method
NumberSeqFormHandler numberSeqFormHandler()
{;
    //you should have been declared numberSeqFormHandler variable in 
    //the ClassDeclaration of your form
    if (!numberSeqFormHandler)
    {
        numberSeqFormHandler = NumberSeqFormHandler::newForm(NumberSequenceReference::find
                        (TypeID2ExtendedTypeId(TypeId(YourExtendedDataType))).NumberSequence,
                                                             element,
                                                             YourDataSourceName,
                                                             fieldnum(YourTableName, Field));
    }
 
    return numberSeqFormHandler;
}
Then you need to actually call the NumberSeqFormHandler class methods like:
//Form methods
public void close()
{
    if (numberSeqFormHandler)
    {
        numberSeqFormHandler.formMethodClose();
    }
    super();
}

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

//DataSource method
public boolean validateWrite()
{
    boolean ret;
 
    ret = super();
    ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) 
    && ret;
 
    return ret;
}

//DataSource method
public void linkActive()
{
    element.numberSeqFormHandler().formMethodDataSourceLinkActive();
    super();
}

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

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

By this you will have your form works efficiently with the Number Sequence engine of Dynamics AX  and you don’t have to write any code at the table level… so remove all that code that you might have written at the initValue method of your table.
One last important thing, NumberSeqFormHandler works with Continuous and non-Continuous number sequences. But if you want to “regenerate” the unused numbers that have been previously generated, you have to set your number sequence as Continuous of course.

2 comments:

  1. Hello Sir,

    Thank you for the post...It was really nice..
    Can you please provide the steps for creating Number Sequence for AX 2012

    I went through MSDN, however it was not useful...

    Regards,
    Amith

    ReplyDelete
  2. Hi Amith,

    You can refer the below aritcles :

    http://technet.microsoft.com/en-us/library/hh242127.aspx

    https://msdax.wordpress.com/2011/09/18/new-number-sequence-implementation-in-dynamics-ax-2012/

    ReplyDelete