Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

how to create auto increment(value) field in table, and share the code?

(0) ShareShare
ReportReport
Posted on by 2,172

In my project i am using the field called AcademicID, which should auto increment the value(integer) for each input given. pls hep me thanks in advance

*This post is locked for comments

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: how to create auto increment(value) field in table, and share the code?

    Two messages above your question is a code sample that shows how to do it without using a number sequence. But please note Martin's comments about not using RecId when determining the latest record. Just pick the largest value in your id field, increment it by one and use this incremented value for your new record.

    But I recommend that you use a number sequence instead.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to create auto increment(value) field in table, and share the code?

    By Number Sequence it's working but Please tell me how to do it by our own code or share the code.

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: how to create auto increment(value) field in table, and share the code?

    It has the same problem as doing it in initValue(). Also the sorting by RecId is wrong - the record with highest RecId doesn't have to have the highest ID.

  • UmesH@ Profile Picture
    UmesH@ 810 on at
    RE: how to create auto increment(value) field in table, and share the code?

    It's Easy To Create in Number Sequence in DataSource.

    Create DataSource Create Method.

    public void create(boolean _append = false)
    {
        int max1;
        A_Marks _Marks;
        str 60 Value;
        str 60 _newCode;
        ;
        //breakpoint;
        super(_append);
        select firstOnly ID from _Marks order by RecId desc;// query to find last number
        if(!_Marks.ID)// When table is blank
        {
            _newCode='MT_'+ strRFix(int2str(1),3, "0");//Assigne value to table field
            DSMarks.ID = _newCode;//Assigne value to table field
        }
        else
        {
            max1=str2int(subStr(_Marks.ID,4,3))+1;// When table is not blank, add 1
            DSMarks.ID ='MT_'+ strRFix(int2str(max1),3, "0");//Assigne value to table field
        }// if Close
    
    }


    Might be Help this Code.

    Thanks.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to create auto increment(value) field in table, and share the code?

    Hi ,

    I need suggestion,

    working on Ax 2009,

    I have a field say F1 and I am using it in a for loop, now my requirement is :

    till the loop runs the F1 value should Increment by 1, say field value F1= A100, if loop run for 5 time, the the field value should be increment like this A101,A102..A105. 

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: how to create auto increment(value) field in table, and share the code?

    Doing it in initValue() would likely lead to duplicate values (or DuplicateKeyException if there is a unique index), because before the record gets written to dataabase, initValue() may be called for other records and they would all get the same value.

  • Anand AX Profile Picture
    Anand AX 2,172 on at
    RE: how to create auto increment(value) field in table, and share the code?

    Dear Mr.Faisal The link u provided is really very helpful, Thanks a lot

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to create auto increment(value) field in table, and share the code?

    as you field AcademicID is a integer field so

    I suggest you should override initvalue() method on the table to write logic to increment the value.

    simple logic would be

    count the number of record in table and

    assign value to AcademicID field by incrementing the total value by 1

    Please verify if this helps you.

  • Verified answer
    Faisal Fareed Profile Picture
    Faisal Fareed 10,794 User Group Leader on at
    RE: how to create auto increment(value) field in table, and share the code?

    Hi Anand, Please have a look on this link which explains step by step how to use number sequence in AX 2012.

    http://daxture.blogspot.co.nz/2014/09/number-sequence-framework-step-by-step.html

    Mark it as an answer if it helps you.

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: how to create auto increment(value) field in table, and share the code?

    Please look at the link in my previous reply.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans