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, ...
Answered

Iterate Datasource records in Reverse.

(0) ShareShare
ReportReport
Posted on by 65

Hello All,

I tried iterating over the datasource records just like the below code snippet shows.

InventTrans                 localInventTrans = InventTrans_DS.getFirst() as InventTrans;
ItemId                      itemId;

while(localInventTrans)
{
  //access fields by using localInventTrans.fieldname
  itemId = localInventTrans.ItemId;


  //get the next record from the datasource
        localInventTrans = InventTrans_DS.getNext() as InventTrans;
}

but the problem is the records are being fetched in reverse, for example:
if the datasource had three records with ids 1,2 and 3. The ds_.getFirst() returns the last id record ( in this case id=3) and then traverses back.

Is there any way I can change this behavior and traverse from the actual first record ( in this case id=1). 

Thanks.

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi,

    are you saying that you get them in different sequence than what you see in the form? Are you 100% sure? For me it returns the records in the same sequence than what I see in the form. So perhaps you're misinterpreting the results.

    Anyway, I don't think that you can impact what the "getFirst" and "getNext" will return. But you can most likely design your own solution in a way that can handle how the system provides them. And you can even take the data source query, add your own sorting logic and then run the query in your code to get the records in the sequence that you want.

    But like I said, to me it seems that the system already works like you are asking. Perhaps there's something wrong with your code. Did you share your actual code with us? Or something else? To me it seems that the code that you shared doesn't do much. 

  • OsamaAG Profile Picture
    65 on at

    Yes nmaenpaa The sequence is reversed from what I see on the form. Not random but totally reverse.

    I am doing code in display method like this :

    displayMethod(MyTable _myTable)
    {
    FormDataSource ds=_myTable.datasource();
    MyTable temp=ds.getFirst() as MyTable;
    
    while(temp)
    {
    
    //// My work here
    
    temp=temp.getNext() as MyTable;
    }
    
    }

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi,

    could you please share some screenshots about the situation? Just to make sure there are no misunderstandings.

    1) A screenshot of the records on the form

    2) Write the ID of each record to infolog in your while loop, and share a screenshot that shows the result

    Thanks!

  • OsamaAG Profile Picture
    65 on at

    nmaenpaa, I debugged the code and from there I identified this issue of records in reverse.

    I would love to provide you with screenshots of the data to better clear the problem but In my case it might not be possible for me. I hope you don't mind.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Also having a display method that iterates every record on the form doesn't sound like a good design.

    Let's say you have a grid with 20 records, and this display field is shown on the grid.

    When the form is opened, the system has to iterate all the records 20 times (one for each time the display method is shown = one for each record visible on the form). This sounds pointless.

    You were discussing this display method requirement in another question, so I don't wish to handle it more here. But I think you might get alternative suggestions for solving your business requirement if you just shared your business requirement there. The quality of help you get (and the quality of the result that you will achieve) depends on the quality of the information that you share when asking for help. And I want to help you achieving best possible quality in your work. 

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    If you can't share any more data, then I'm unable to provide any more help.

    I hope you'll find a solution to your business requirement, whatever it may be.

    Have a nice day!

  • OsamaAG Profile Picture
    65 on at

    Yes you are absolutely right that my display method is actually doing exactly like your example and its not efficient in any way. but as I don't have expertise in D365 I am stuck in using display method

    Lets say I am standing at record 100th, I have to get the Amount sum of all the previous records and display it in the records 100th.

    If I was standing on 4th record then I'll have to get the Amount sum of the previous records and that would be sum of 3 records.

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Here's my suggestion: if the user wants to see a sum of some columns, they can export the data to Excel, and simply highlight all the columns that they want to sum. Excel shows the sum of the selected cells automatically.

    I'm not sure if there's any nice way to show such "running sum" of all previous rows without a performance disaster. So perhaps you need to rethink this a bit.

    Depending on the actual needs of the users, they could also use the new grid capabilities that have recently been added to D365FO : https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/fin-ops/get-started/grid-capabilities

    But only if they use D365FO. You posted in AX forum, and used "Dynamics AX (current version)" tag so I'm not sure which version you are using.

  • OsamaAG Profile Picture
    65 on at

    Yes you are right. I did include the "running sum" but at the sacrifice of performance. I am looking for a better way.

    Thanks a lot for your suggestion and replies. nmaenpaa.

  • Suggested answer
    OsamaAG Profile Picture
    65 on at

     nmaenpaa   As for the problem that I was facing  of  datasource records are in reverse.

    This was because my Datasource property "Start position" was set to "Last".

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
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans