Blog

Microsoft Dynamics AX Thoughts

3

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.

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

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

1
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//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:

1
2
3
4
5
6
7
8
9
//Form methods
public void close()
{
    if (numberSeqFormHandler)
    {
        numberSeqFormHandler.formMethodClose();
    }
    super();
}
1
2
3
4
5
6
//DataSource method
public void write()
{
    element.numberSeqFormHandler().formMethodDataSourceWrite();
    super();
}
1
2
3
4
5
6
7
8
9
10
//DataSource method
public boolean validateWrite()
{
    boolean ret;
 
    ret = super();
    ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) && ret;
 
    return ret;
}
1
2
3
4
5
6
//DataSource method
public void linkActive()
{
    element.numberSeqFormHandler().formMethodDataSourceLinkActive();
    super();
}
1
2
3
4
5
6
//DataSource method
public void delete()
{
    element.numberSeqFormHandler().formMethodDataSourceDelete();
    super();
}
1
2
3
4
5
6
7
8
9
//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.

3 Comments to “How to Use Number Sequence Engine Efficiently with Dynamics AX Forms”

  • Nice post and indeed the way to use the numbersequence engine. One thing though : “you don’t have to write any code at the table level”

    Well this actually is a best practice to write logic as close to the table as possible. What if I call inserts etc.. from code on several places, then I put the code on the table and don’t need to worry it won’t have a number..

    And I always put code that would be on the table level in some kind of a class structure called tablehandlers so that the logic is seprated from standard code.

    Just a thought…

  • Thank you very much for your comment Saelen. And I do like your thoughts :) .

    Well, you’re absolutely right. You have to write code that is related to table in the closest place to table.

    But my concern actually was how to deal with the Number Sequence from a form level. And this is how the Dynamics AX standard code is actually written. In some forms that use the NumberSeqFormHandler class, you will not see any number sequence code at the table level.
    But the question here, how should we recover creating new records in that table from any other place out of its form (that uses NumberSeqFormHandler)? The answer here is to use a “middleware” class that will take care of creating new records in the table. A very good example for this is the SalesTableType class that takes care of all the validations/concentrates/events handling for that table.

    Again, it is highly important to write your code in the closest place to your table.. as you said Saelen.

    Thank you again! :)

  • Again a very good post Amer… Thanks for helping me out with this post again….

Post comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Dynamics AX 2012 Event

Recent Posts

Tags

Archives

Random Testimonial

  • ~ Mohammad A-Awami, Dynamics AX Specialist at WorleyParsons

    AlAwami"I worked with Amer for WorleyParsons. I found him very cooperative. He is able to help me in a lot of issues that related to ax. His ability to solve the problems is fascinating me. It is so fast and done on the best practice. His ability on business analysis and implantation are done on the best methods."

  • Read more testimonials »

Posting tweet...

Powered by Twitter Tools