Adding extension to Preview Posting Business Central AL Extension

jhanvincent14jhanvincent14 Member Posts: 214
Hi,

I have an event EventSubscriber for SampleTable OnAfterInsertEvent to populate the tempSampleEnty temp table and another EventSubscriber to another Codeunit 20 (Posting Preview Event Handler) for the event "OnAfterFillDocumentEntry" to add new row in POSTING PREVIEW window but my temporary table is uninitialized or empty when Im using it in InsertDocumentEntry function.

Everytime I do debug. my tempsapleentry table is always empty on OnAfterFillDocumentEntry

Here is my code:
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;
}

Best Answer

  • MariosopenaMariosopena Member Posts: 6
    edited 2022-06-07 Answer ✓
    Hi, @jhanvincent14 I've found the solution. Set the 'SingleInstance' property of your Codeunit to true. It should do the trick ;)

    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.

Answers

  • PascualPascual Member Posts: 9
    Hi, try initialize to the tempSampleEnty table, do a FINDSET.tempSampleEnty, for example.
  • MariosopenaMariosopena Member Posts: 6
    Hi, @jhanvincent14 I have the same problem, have you found the solution for this?
  • Developer101Developer101 Member Posts: 528
    I am not sure if a temporary table could be used in that way. I have not seen any example. Is there any example in the standard?
    United Kingdom
  • MariosopenaMariosopena Member Posts: 6
    edited 2022-06-07 Answer ✓
    Hi, @jhanvincent14 I've found the solution. Set the 'SingleInstance' property of your Codeunit to true. It should do the trick ;)

    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.
  • Developer101Developer101 Member Posts: 528
    Thanks @Mariosopena
    United Kingdom
  • @jhanvincent14 did you finished that code. if you have the correct code so pls give me i need . pls helpme
Sign In or Register to comment.