web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to autoIncrement a field in function of other value

(0) ShareShare
ReportReport
Posted on by 15

Hello !

i'm creating a form in order to write data in a table and show it in the form . That form only show the data related to a number order and sort by a specific int called ordre. (we first show the line with ordre =1, then ordre =2 ..) . 

So what I want is that when I create a new line in the form,  the value of ordre is automatically calculated and displayed. I put some pictures next to it so you can understand better. I saw some topics about using LineNum but I don't think it's relevant to my case because the number ordre depends on the ordernumber and not on the LineNum.

Here is what i develop for the moment : ( in the methods of the table linked to the form) :

public void initValue()
{
    SLF_ordonnancement SLF_ordonnancement;
    int value;
    
    super();
    
    select firstOnly SLF_ordonnancement order by SLF_ordonnancement.ordre desc
    where SLF_ordonnancement.CodeBarre == SLF_ordonnancement.CodeBarre;
    {
         value = SLF_ordonnancement.ordre   1;
    }
    
    SLF_ordonnancement.ordre = value;
   info(int2str(SLF_ordonnancement.ordre));
}
pastedimage1625746788233v2.png

I have the same question (0)
  • atajjo Profile Picture
    509 on at

    Your requirement sounds very similar to the idea of LineNum, could you clarify more regarding why you can't use it?

    Btw as an example, here's how LineNum is assigned for SalesLine in D365

    if (_salesLineCreateLineParameters.setLineNum && !this.LineNum)
            {
                this.LineNum = SalesLine::lastLineNum(this.SalesId)   1.0;
            }

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    There are quite a few bugs.

    The where condition makes no sence, because you're comparing a field value with itself. You need to take into account the code of the current record. If it's a table method, you can refer to the current record by this variable:

    SLF_ordonnancement ordonnancement;
        
    select maxOf(ordre) from ordonnancement
    	where ordonnancement.CodeBarre == this.CodeBarre;

    Note that I simplified by your code by using maxOf() aggregation function. You also fetched all fields, although you need only one of them (ordre).

    But doing it in initValue() is likely wrong. Are you sure that CodeBarre is already filled in at this point?

    You're also assigning the value to a wrong buffer. You're setting it into SLF_ordonnancement.ordre, but it's a local variable that ceases to exist at the end of the method. Again, if it's in a table method, use this.ordre instead:

    this.ordre = ordonnancement.ordre   1;

    Note that it may be wiser to assign the value at the latest possible time - when actually inserting the record to database. It minimizes the risk of conflicts (when multiple records are created concurrently and get the same number).

  • AymericKer Profile Picture
    15 on at

    I thought i coudn't use it because i believed LineNum was for a the record and couldn't be in function of an other variable. But in your example it seems to correspond to the situation.

  • AymericKer Profile Picture
    15 on at

    Thank you for your answer. I just checked and your right, CodeBarre isn't filled at this point, it's only filled when the line is created in the table so after i created the line in the form. How can i solved that ? 

    Also, CodeBarre is equal to SalesTable.SalesId by a relation in the table. So maybe, i can go find the SalesTable.SalesId related ?

    Just a little question, should i write this code directly in the table ? Or in the datasource of the form ? I don't really understand the difference between those.

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    You can intialize the field anywhere between the place where CodeBarre gets populated and the physical insert of the line to database.

    One option is simply doing it in insert(), as I suggested.

    If you want to do it earlier, you must find out where CodeBarre gets set, so you can initialize ordre somewhere after that.

    Unfortunately I don't understand your remarks about LineNum.

  • AymericKer Profile Picture
    15 on at

    Here is what i tried :

    I put it in the method of the form when i'm creating the line, so just before CodeBarre gets set in the table SLF_ordonnancement. But their is nothing showed on my screen so i think i'm not selecting the SalesId but i do'nt know why 

    public void init()
    {
        super();
    
        SLF_ordonnancement.CodeBarre = SalesTable.SalesId;
        SLF_ordonnancement.insert();
        info("SalesId : "   SalesTable.SalesId);
    }

  • AymericKer Profile Picture
    15 on at

    Here is what i tried : 

    public void init()
    {
        super();
    
        SLF_ordonnancement.CodeBarre = SalesTable.SalesId;
        SLF_ordonnancement.insert();
        info("SalesId : "   SalesTable.SalesId);
    }

    I put that code in the methods of the form, so when i'm creating the screen, the form is going to take the SalesId and put it in SLF_ordonnancement before we create a new line.

    But stil does't seems to work, when i info the value of SalesTable.salesId their is nothing showed on the screen.

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    init() method of the form is a completely wrong place. The purpose of this method is initializing the form before even opening it - initializing controls, the query for fetching the data and so on. It's not related to your case, no data is loaded at this point and and inserting records here doesn't make much sense.

    Please use the debugger to test your code. For example, you'll immediately see that salesTable variable isn't populared at all (before the form wasn't run yet) and therefore no code depending on it can work.

    But why don't you simply follow my suggestion and don't set the field value in insert() method (before calling super())? Aren't you just making things more complicated for yourself?

  • ergun sahin Profile Picture
    8,826 Moderator on at

    I did not read the details of what was written, but if you want to give the first value, you can do it in the form datasource initvalue method.

    (Of course, you must have a salesTable record)

    docs.microsoft.com/.../dynamics.ax.application.formdatasource.initvalue

  • AymericKer Profile Picture
    15 on at

    I tried to follow your suggestion but I didn't quite understand where to write it. Normally I followed your advice :

    public void initValue()
    {
        SLF_ordonnancement SLF_ordonnancement;
        SalesTable         SalesTable;
        
        this.CodeBarre = SalesTable.SalesId;
        this.insert();
    
        super();
    
        select maxOf(ordre) from SLF_ordonnancement
        where SLF_ordonnancement.CodeBarre == SalesTable.SalesId;
    
        this.ordre = SLF_ordonnancement.ordre  1;
    }

    But I'm not sure the code is in the right place. This one is in the table SLF_ordering which is called when the row is already created. ( so too late). Maybe this one should be placed in a method of the form?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 490 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans