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

Notifications

Announcements

Community site session details

Community site session details

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

Form: After manual line insert, Refresh moves cursor to another record

(2) ShareShare
ReportReport
Posted on by 179

Scenario

I have a custom Dynamics 365 Finance & Operations form with a Header / Line pattern.


  • Header datasource: OrderHeader

  • Line datasource: OrderLine

  • The Line datasource is displayed in a grid.

I am not using initValue(), and I am not using DataSource.create().

I intentionally insert the record manually.

 

How I create a new line
 

public void newLine()
{
    FormDataSource headerDs =
        this.dataSource(formDataSourceStr(MyForm, OrderHeader));
    FormDataSource lineDs =
        this.dataSource(formDataSourceStr(MyForm, OrderLine));
    OrderHeader header = headerDs.cursor();
    OrderLine   line;
    if (!header || !header.OrderId)
    {
        warning("Please create header first.");
        return;
    }
    ttsbegin;
    line.clear();
    line.OrderId   = header.OrderId;
    line.SomeCode  = header.SomeCode;
    line.insert();
    ttscommit;
    // Force UI to show the new record
    lineDs.research(true);
    lineDs.findRecord(line);
}
 
 

This works correctly:


  • The record is inserted

  • The grid scrolls to the new line

  • The cursor is positioned on the new record

The problem

After that:


  • When I press the Refresh button on the form (standard toolbar refresh),

  • The grid reloads,

  • And the cursor jumps to another existing line, not the one I just inserted.

The data is correct in the database, but the active row selection is lost.

Important notes


  • I deliberately use clear() + insert() (not DataSource.create()).

  • I already call research(true) and findRecord() after insert.

  • There is no custom executeQuery() logic involved.

  • I am using RecId-based selection (no custom LineNo).

Questions


  1. Why does pressing Refresh cause the cursor to move to a different line?

  2. How does D365FO decide which record becomes the active row after a refresh?

  3. Is there a recommended pattern to:

    • insert a line manually,

    • position the cursor on it,

    • and keep it selected after Refresh 

  4. What should I check (index, sort order, datasource properties, grid behavior) to diagnose this?

Any guidance on the correct pattern or internal behavior would be appreciated.

Categories:
I have the same question (1)
  • Suggested answer
    Raj Borad Profile Picture
    1,444 on at
    Hi,
     
    I just tried the same with the Sales order line creation, I created 3 lines and click "Add line" button, it added the new black line. 
    After I pressed the refresh button (in action bar), the selected record moved to first line of the sales order.
    May be that is the standard behavior.
     
    Thanks.
     
  • Suggested answer
    Martin Dráb Profile Picture
    238,054 Most Valuable Professional on at
    How you insert the line (and therefore all the code) isn't really relevant to your questions. What you see is the expected behavior of the standard logic. You can try it in standard forms, such as purchase orders. Select a line (not the first one) and press Refresh.
     
    I'm assuming that your data sources are delayed-linked.
     
    1. When the header gets refresh and the query for lines gets executed, the information about the selected line isn't preserved.
    2. Either the first or the last record gets selected, depending on the configuration of the data source.
    3. As I already mentioned, it has nothing to do with line insertion; the problem is the same even if you select an existing line and no insertion is involved. You'd need to change the logic of the data refresh action.
    4. Nothing to check - it's the expected behavior.
     
    Although your code isn't related to your question, let me use this opportunity to show you how you can improve it.
     
    A half of your code can be thrown away when you realize that the form already contains (automatically created) variables for data sources and their buffers. Let's use them:
    public void newLine()
    {
        if (!orderHeader.OrderId)
        {
            warning("Please create header first.");
            return;
        }
    
        orderLine.clear();
        orderLine.OrderId   = header.OrderId;
        orderLine.SomeCode  = header.SomeCode;
        orderLine.insert();
    	
        // Force UI to show the new record
        orderLine_ds.research(true);
        orderLine_ds.findRecord(orderLine);
    }
    You also should call validateWrite() method, to verify that the record can be saved, otherwise you risk inserting inconsistent data. You should move your validation of OrderId there, if you require using your custom message instead of just letting the standard validation handle it for you.
  • Statics 365 Profile Picture
    179 on at
    Thanks for the explanation. I understand that this is expected behavior in the framework.

    My requirement is just to keep the same header record after Refresh.

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

News and Announcements

Season of Giving Solutions is Here!

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
Abhilash Warrier Profile Picture

Abhilash Warrier 678 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 283 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans