SQL Timestamp cannot be added to Table Extension
1. Describe the bug Trying to use "SQL Timestamp" for a Data Replication Module. But because this cannot be added to Table Extensions, it makes using the field in Business Central impossible.
I tried also to copy the SQL Timestamp value in "OnAfterInsert/Modifi/Rename" events, but this did not work because of conflicting event subscribers.
For example "Price List Line": In BC18 you added event subscriber for OnBeforeModify Price List Line. *** codeunit 7017 "Price List Management" ***
[EventSubscriber(ObjectType::Table, Database::"Price List Line", 'OnBeforeModifyEvent', '', false, false)]
local procedure OnAfterCopyToPriceSource(var Rec: Record "Price List Line"; var xRec: Record "Price List Line"; RunTrigger: Boolean);
begin
if Rec.IsTemporary() then
exit;
MarkLineAsDraft(Rec, xRec);
end;
If I write code to copy SQL Timestamp like this:
[EventSubscriber(ObjectType::Table, Database::"Price List Line", 'OnAfterModifyEvent', '', false, false)]
local procedure UpdateReplicationCounterOnAfterModifyPriceListLine(var Rec: Record "Price List Line"; var xRec: Record "Price List Line"; RunTrigger: Boolean)
var
DataTypeMgmt: Codeunit "Data Type Management";
RecRef: RecordRef;
begin
IF Rec.IsTemporary() then
exit;
IF Rec.IsUpdatingReplicationCounter() then begin // code must run also when RunTrigger is false. This is to avoid infinite loop
Rec.SetIsUpdatingReplicationCounter(false);
Exit;
end;
IF DataTypeMgmt.GetRecordRef(Rec, RecRef) THEN begin
UpdateReplicationCounter(RecRef, Rec.FieldNo("NPR Replication Counter"));
RecRef.SetTable(Rec);
Rec.SetIsUpdatingReplicationCounter(true);
Rec.Modify(false);
end;
end;
Where UpdateReplicationCounter function is like this:
procedure UpdateReplicationCounter(RecRef: RecordRef; ReplicationCounterFieldNo: Integer)
var
FRefSQLTimeStamp: FieldRef;
FRefReplicationCounter: FieldRef;
begin
FRefSQLTimeStamp := RecRef.Field(0); //SQL Timestamp
FRefReplicationCounter := RecRef.Field(ReplicationCounterFieldNo);
FRefReplicationCounter.Value := FRefSQLTimeStamp.Value;
end;
This would not work because the code you added in "OnBeforeModify" will be triggered again and will always change back the status of the "Price List Line" from "Active" to "Draft"..
So, this basically means that How to use SQL Timestamp is unusable in a real life scenario at the moment.. Because we would need the field into all tables, not just in the custom ones..
2. To Reproduce Steps to reproduce the behavior:
In a Table Extension try to add "SQL Timestamp":
field(6014405; "NPR Replication Counter"; BigInteger)
{
Caption = 'Replication Counter';
SqlTimestamp = true;
}
This fails with error: Property 'SqlTimestamp' is not allowed on a table extension.AL(AL0404)
3. Expected behavior We don't understand why is not possible to add this field to both Tables and Table Extensions. Obviously, the expected behavior is to be able to do that, so we can use the field in real life scenario.
4. Actual behavior As explained above.
5. Versions: All BC Versions
Answers to your questions available here https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-table-system-fields
Hi, I know that article, but it does not solve what I described in this ticket:
The problem is that we need to do this: "A typical use of the timestamp field is for synchronizing data changes in tables. It lets you identify records that have changed since the last synchronization. For example, you can read all the records in a table, then store the highest timestamp value. Later, you can query and retrieve records that have a higher timestamp value than the stored value."
But we cannot because the field cannot be added to Table Extensions.. So basically it is unusable as explained above in this ticket. (we need to synchronize all tables including the standard ones)
Unfortunately, the platform does not support defining multiple timestamp fields to a single table, which could happen if we supported adding timestamp fields to table extensions (since the table + table extensions is merged into one table at runtime).
We are planning to support reading the timestamp field as a normal field from any table in a future version but I cannot give you a timeline for this.
However, I am not sure what it is you need to accomplish that cannot be done by reading the timestamp with the RecordRef workaround you listed (getting field 0), but would be possible if you could add the timestamp field? The timestamp field you add yourself is simply an alias to the same timestamp field you are reading with RecRef.Field(0), so it would return the sasme value.
Yes, will work also to read the value directly in the API pages.. But there are downsides: For example, we cannot add an index on the field so sorting to get the most recent records (which have timestamp greater than a timestamp we saved in previous run) would be slow.
Unfortunately, the platform does not support defining multiple timestamp fields to a single table, which could happen if we supported adding timestamp fields to table extensions (since the table + table extensions is merged into one table at runtime).
We are planning to support reading the timestamp field as a normal field from any table in a future version but I cannot give you a timeline for this.
However, I am not sure what it is you need to accomplish that cannot be done by reading the timestamp with the RecordRef workaround you listed (getting field 0), but would be possible if you could add the timestamp field? The timestamp field you add yourself is simply an alias to the same timestamp field you are reading with
RecRef.Field(0), so it would return the sasme value.
@jehelles: would one option be that you expose it on all tables by default, just like the other new system fields? You might even be retrieving it already in your NST data stack, depending on how you check stale records, in which case it would not be a change impacting your "query engine" at all? I know that doesn't make it easy, but just saying :) This would also prevent any of us partners from needing a tableextension just to add a "fake field", ideally we don't want additional joins.
On this same topic, it should be mentioned that the reason we are not eagerly jumping on "Last date modified" with an index added on it, is because it is not clear to us if we can trust the value of this timestamp to be guaranteed to monotonically increase even with load balanced webservice NSTs? It is undocumented by MS if it is based on database server time with this guarantee or if it is pulled by the processing NST in which case I assume there is zero guarantee and "SQL timestamp" with an index is still better for robust data sync?
Please advise for how to do performant index-covered data replication best as a partner in a cloud compliant app :)
Yes, will work also to read the value directly in the API pages.. But there are downsides: For example, we cannot add an index on the field so sorting to get the most recent records (which have timestamp greater than a timestamp we saved in previous run) would be slow.
I see, yes, this is a scenario where having an actual field will help, but again, we would rather focus on the "right" solution, exposing the timestamp field on all tables instead of opening the SqlTimestamp = true; up for table extensions
@jehelles: would one option be that you expose it on all tables by default, just like the other new system fields? You might even be retrieving it already in your NST data stack, depending on how you check stale records, in which case it would not be a change impacting your "query engine" at all? I know that doesn't make it easy, but just saying :) This would also prevent any of us partners from needing a tableextension just to add a "fake field", ideally we don't want additional joins.
Yep, this is exactly what I had in mind - and yes, we already have the data and 'internal field (as you can see when you do Field(0), but it still requires work to make sure everything works from dev environment and all the way through to the UI.
On this same topic, it should be mentioned that the reason we are not eagerly jumping on "Last date modified" with an index added on it, is because it is not clear to us if we can trust the value of this timestamp to be guaranteed to monotonically increase even with load balanced webservice NSTs? It is undocumented by MS if it is based on database server time with this guarantee or if it is pulled by the processing NST in which case I assume there is zero guarantee and "SQL timestamp" with an index is still better for robust data sync?
Please advise for how to do performant index-covered data replication best as a partner in a cloud compliant app :)
Good point, we should probably document this better, it is indeed assigned by the NST. You can look at the CRM synchronization engine in the base app for inspiration on how last modified can be used to do data synchronization until the timestamp becomes available.
it is indeed assigned by the NST. You can look at the CRM synchronization engine in the base app for inspiration on how last modified can be used to do data synchronization until the timestamp becomes available.
@jehelles Thanks for the answer! Although it is the one I feared a little bit - if the "Last Modified At" field is populated by each NST server that means using it as an external API consumer would be like trusting the clocks of any number of load balanced NST servers to be in sync down to millisecond precision. I don't think that assumption is robust in the real world across distributed servers, see things like below articles for examples of what I mean:
https://martinfowler.com/articles/patterns-of-distributed-systems/lamport-clock.html https://docs.microsoft.com/en-us/windows-server/networking/windows-time-service/support-boundary
If the CRM integration in BaseApp is built upon that assumption, I'd imagine it is not robust. I've skimmed the code for it now and found the two functions below:
CRMIntegrationRecord.IsModifiedAfterLastSynchonizedCRMRecord(): This one compares the previous "Last Modified At" with the value on record, using the below helper function.
TypeHelper.CompareDateTime(): This one has the following comment: // The threshold must be used to compensate for the varying levels of precision // when storing DateTime values. An example of this is the T-SQL datetime type, // which has a precision that goes down to the nearest 0, 3, or 7 milliseconds
Meaning, the timestamp comparison used in CRM is allowing time drift of up to 10 milliseconds for different reasons, but in this clock drift context the 10 milliseconds is nothing more than a magic number, it doesn't make it robust, just less likely to hit an issue. On top of that, when polling an odata list API from the outside and specifying a timestamp filter directly as an odata filter parameter with a (minus 10ms or more) grace window like this, that would also cause us to never receive an empty dataset even when nothing has actually changed.
If MS, the BC team, is recommending that partners use distributed clocks to synchronize our data replication, I'd prefer to have that in clear writing on MS docs so I can point to you guys if it ever fails - especially since it'll be running on your hardware which is completely outside of our control and knowledge :) Otherwise, we are probably going with "SQL timestamp" for our module, even if we have to live without an index for now, but in the past this was a complete non-issue for us as we could add an index on it.
I don't mean to be pedantic, I hope you understand where I'm coming from and please let me know if you think I am missing something - I understand this might be quite a bit down the backlog but I think it's a serious platform gap and the documentation should be steering partners in the right direction here. Especially because data sync. is such a common thing these days.
Can we close this request for now?
I understand that resolution is that: Microsoft will focus on the "right" solution, exposing the timestamp field on all tables.
So, if this was added to your backlog and we can expect to have it fixed in a future release, I guess this can be closed for now.
Just for tracking purposes/voting, idea was added by Andrei-Ovidiu for this https://experience.dynamics.com/ideas/idea/?ideaid=5c828c36-bb16-ec11-b76a-0003ff45d863