[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.
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.
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
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.
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.
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?
What about adding the code on the OnUpdating event handler at SalesLine table instead of form.
Thanks,
Girish S.
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.
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.
I have tried updating as you suggested, but I still get the same error. Do you have any other suggestions?
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.
André Arnaud de Cal...
291,965
Super User 2025 Season 1
Martin Dráb
230,817
Most Valuable Professional
nmaenpaa
101,156