Skip to main content

Notifications

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.

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Potential data loss with reread() on a datasource?

    You can check if there is any extension class for Sales table form, sales line datasource. If not in your custom model then probably in third party ISV.

  • ChosenSilver Profile Picture
    ChosenSilver 50 on at
    RE: Potential data loss with reread() on a datasource?

    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

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Potential data loss with reread() on a datasource?

    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
    ChosenSilver 50 on at
    RE: Potential data loss with reread() on a datasource?

    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
    Mohit Rampal 12,552 Super User 2024 Season 1 on at
    RE: Potential data loss with reread() on a datasource?

    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?

  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Potential data loss with reread() on a datasource?

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

    Thanks,

    Girish S.

  • ChosenSilver Profile Picture
    ChosenSilver 50 on at
    RE: Potential data loss with reread() on a datasource?

    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
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Potential data loss with reread() on a datasource?

    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
    ChosenSilver 50 on at
    RE: Potential data loss with reread() on a datasource?

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

  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Potential data loss with reread() on a datasource?

    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.

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…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans