codeunit 99999 "SampleTableHandler" { var tempSampleEnty : Record "Sample Table" temporary; PostPrevHandler : Codeunit "Posting Preview Event Handler"; [EventSubscriber(ObjectType::Table, 99991, 'OnAfterInsertEvent', '', true, true)] procedure OnInsertSampleEntry(VAR Rec: Record "SampleTable"); begin if Rec.IsTemporary then exit; PostPrevHandler.PreventCommit(); tempSampleEnty := Rec; tempSampleEnty."Document No." := '***'; tempSampleEnty.INSERT; end; [EventSubscriber(ObjectType::Codeunit, 20, 'OnAfterFillDocumentEntry', '', true, true)] procedure InsertSampleDocumentEntry(VAR DocumentEntry: Record "Document Entry"); begin PostPrevHandler.InsertDocumentEntry(tempSampleEnty, DocumentEntry); end; }
Answers
Remember to 'clear' your temporary variable before starting the posting preview as it is instanciated once.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post Preview", 'OnBeforeRunPreview', '', true, true)]
local procedure OnBeforeRunPreview(Subscriber: Variant; RecVar: Variant)
var
SampleTableHandler: Codeunit "SampleTableHandler";
begin
SampleTableHandler.ClearTempEntries();
end;
I said 'clear' because Clear(tempSampleEntry) is not enough. You must Delete all records from your temp variable.