Recover auto increment Id DataSnap Delphi XE3

I use Delphi XE3 / DataSnap with Firedac

When I write the parent table to the server I want to retrieve the value that was added via auto increment in the parent so that I can report this new value in the foreign key in the child table.

I do not know where to catch and how to catch if it can be in what event.

...AfterUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);

Or

...BeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
  var Applied: Boolean);
Author: Ailton, 2014-06-10

2 answers

On the server (TADQuery and TDataSetProvider), in the 'Afterpost' event of the TADQuery Master must run the Refresh command that will be updated with the ID of the AUTOINCREMENT that was just executed "I created a variable FId: Integer to receive the value returned from the bank for use in the child dataset"

As the example below:

procedure TsrmNFSe.ADQueryAfterPost(DataSet: TDataSet);
begin
   ADQuery.Refresh;
   FId    := ADQuery.fieldbyname('id_campo_autoincremento').AsInteger;
end;
 1
Author: Ailton, 2014-06-11 19:08:06

The best way for you to do this is by making use of event [BeforeUpdateRecord][1]. In this event you can change the Delta that is being sent to the bank (even controlling the type of change that is happening. You can save the new ID inside a DM private variable (or parent object) and then use it in items.

 0
Author: Francisco Thiago, 2017-12-22 03:23:54