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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Potential data loss with reread() on a datasource?

(0) ShareShare
ReportReport
Posted on by 50

[FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Writing)]
    public static void SalesLine_OnWriting(FormDataSource _sender, FormDataSourceEventArgs _e)
    {
        SalesTable salesTable = _sender.formRun().dataSource(formDataSourceStr(SalesTable, SalesTable)).cursor();
        SalesLine salesLine = _sender.cursor();


        if (salesLine.DeliveryPostalAddress != salesTable.DeliveryPostalAddress)
        {
            ttsbegin;
            salesLine.selectForUpdate(true);
            salesLine.DeliveryPostalAddress = salesTable.DeliveryPostalAddress;
            salesLine.update();
            ttscommit;
        }
    }

Our users have been getting an error, "Cannot edit a record in Order lines (SalesLine). The record shown has been updated by another user." Through debugging, I have narrowed the error down to be coming from the event handler shown above. Is there any potential data loss that could happen if I refresh the datasource just before the ttsbegin?

[FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Writing)]
    public static void SalesLine_OnWriting(FormDataSource _sender, FormDataSourceEventArgs _e)
    {
        SalesTable salesTable = _sender.formRun().dataSource(formDataSourceStr(SalesTable, SalesTable)).cursor();
        SalesLine salesLine = _sender.cursor();


        if (salesLine.DeliveryPostalAddress != salesTable.DeliveryPostalAddress)
        {
            salesLine.reread(); //could this potentially overwrite data in the cursor?
            
            ttsbegin;
            salesLine.selectForUpdate(true);
            salesLine.DeliveryPostalAddress = salesTable.DeliveryPostalAddress;
            salesLine.update();
            ttscommit;
        }
    }

If I change the code as above then I no longer get the error. Something in the back of my head tells me that this approach could be dangerous. Looking for some insight from the experts.

I have the same question (0)
  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    Hi, You don't need to update SalesLine table in this event Handler. Just mapping the value in field in SalesLine table should work. This event handler will call update method and since you have written code to update, that's why system is throwing error that record is been updated by another user.

    Please  try below code.

    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Writing)]
        public static void SalesLine_OnWriting(FormDataSource _sender, FormDataSourceEventArgs _e)
        {
            SalesTable salesTable = _sender.formRun().dataSource(formDataSourceStr(SalesTable, SalesTable)).cursor();
            SalesLine salesLine = _sender.cursor();
    
            if (salesLine.DeliveryPostalAddress != salesTable.DeliveryPostalAddress)
            {
                salesLine.DeliveryPostalAddress = salesTable.DeliveryPostalAddress;
            }
        }

  • GirishS Profile Picture
    27,833 Moderator on at

    Hi,

    Your code is totally wrong. Since SalesLine table you are updating is part of form datasource, Calling reread and update method is unnecessary. Its just method sequence to call the written method before updating the record in the table. So only assign values to the field as Mohit mentioned.

    Thanks,

    Girish S.

  • ChosenSilver Profile Picture
    50 on at

    I have tried updating as you suggested, but I still get the same error. Do you have any other suggestions?

  • GirishS Profile Picture
    27,833 Moderator on at

    If you are assigning DeliveryPostalAddress from SalesTable header to SalesLine line table, then you can go for initValue method on SalesLine  form DataSource. InitValue method will be called when creating new record on the SalesLine table.

    Thanks,

    Girish S.

  • ChosenSilver Profile Picture
    50 on at

    In this case we are not setting the initial value, and the event handler is being called after updates have been made on the form. Specifically the retail channel has been changed, which causes a lot of other updates to happen before we arrive here.

  • GirishS Profile Picture
    27,833 Moderator on at

    What about adding the code on the OnUpdating event handler at SalesLine table instead of form.

    Thanks,

    Girish S.

  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    Have you debugged update method in SalesLine table? It must be calling twice, if yes, then check call stack for the root cause. If you comment this method, do you still get the error?

  • ChosenSilver Profile Picture
    50 on at

    Looking at this, I can see that after all of the updates are done from the retail channel being changed, the research() method is being called on the datasource. This seems like it should be correct, but for some reason something in that research() method is causing the update() to be called on the SalesLine again, as you suspected. I am continuing to debug in this area, but so far I am not seeing anything in there that should trigger an update.

    I do see an event handler for the Refreshed event of the SalesTable, but all it does is enable/disable some controls and not actually update any data.

  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    In that case, call stack will help you to trace the root cause. Research method don't call update unless custom code is written. The code provided in first comment should work when you fix the call to update method.

  • ChosenSilver Profile Picture
    50 on at

    Sorry to keep bothering you about this. I am reviewing the call stack and it does not show anything being updated. From the time research() is called, there are only two things above that in the stack: SalesLine.executeQuery() and a CoC on SalesLine.validateWrite() which means at that point it is already trying to run the second update

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 590 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 349

#3
Diego Mancassola Profile Picture

Diego Mancassola 263

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans