web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Need Help Understanding behaviors of Code Unit "Sales Post Invoice Events"

(9) ShareShare
ReportReport
Posted on by 457
Hello Mentors,
 
Seeking help on below project trying to build portfolio on various business scanarios's.
 
I am trying to achieve that whenever a sales invoice or sales credit note is being posted and if a customer has E-Commerce Partner ID  defined in master (which also is a customer)
 
I want to add one line with  reverse amount into current customer and then second line as current transaction amount to the E-Commerce Partner ID (similar as payment method code with a balancing GL Assigned).

When below code is executed second GenJnlLine is being posted as expected how ever Inventory GL is not being posted and first line also not gets posted.
 
I am unable to figure out what is causing this.

Please share your valuable insights. 
 
(Also amount is just for testing purpose, it needs to be  as per customer ledger entry amount.)
 
 
 [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales Post Invoice Events", OnAfterPrepareGenJnlLine, '', false, false)]
    procedure PostAdditionalJournalEntry(
        SalesHeader: Record "Sales Header";
        var GenJnlLine: Record "Gen. Journal Line";
        InvoicePostingBuffer: Record "Invoice Posting Buffer" temporary)
    var
        GenJnlPostLine: Codeunit "Gen. Jnl.-Post Batch";
        Customer: Record Customer;
        lineNo: Integer;
        CU: Codeunit "Gen. Jnl.-Post Batch";
 
    begin
        Customer.SetRange("No.", SalesHeader."Sell-to Customer No.");
        if Customer.FindFirst() then begin
            if Customer."E-Commerce Partner ID" <> '' then begin
                lineNo := GenJnlLine.GetNewLineNo(GenJnlLine."Journal Template Name", GenJnlLine."Journal Batch Name");
                GenJnlLine."Line No." := lineNo;
                GenJnlLine."Posting Date" := SalesHeader."Posting Date";
                GenJnlLine."Account Type" := GenJnlLine."Account Type"::Customer;
                GenJnlLine."Account No." := SalesHeader."Sell-to Customer No.";
                GenJnlLine.Amount := InvoicePostingBuffer.Amount;
                GenJnlLine."Shortcut Dimension 1 Code" := SalesHeader."Shortcut Dimension 1 Code";
                GenJnlLine."Shortcut Dimension 2 Code" := SalesHeader."Shortcut Dimension 2 Code";
                GenJnlLine."Dimension Set ID" := SalesHeader."Dimension Set ID";
                Clear(GenJnlLine."Gen. Posting Type");
                Clear(GenJnlLine."Gen. Bus. Posting Group");
                Clear(GenJnlLine."Gen. Prod. Posting Group");
                Clear(GenJnlLine."VAT Bus. Posting Group");
                Clear(GenJnlLine."VAT Prod. Posting Group");
                Clear(GenJnlLine."Document Type");
                Clear(GenJnlLine."Bal. Account Type");
                Clear(GenJnlLine."Balance Account Id");
                GenJnlLine.Insert();
                lineNo := 0;
                lineNo := GenJnlLine.GetNewLineNo(GenJnlLine."Journal Template Name", GenJnlLine."Journal Batch Name");
                GenJnlLine."Line No." := lineNo;
                GenJnlLine."Posting Date" := SalesHeader."Posting Date";
                GenJnlLine."Account Type" := GenJnlLine."Account Type"::Customer;
                GenJnlLine."Account No." := Customer."E-Commerce Partner ID";
                GenJnlLine.Amount := -InvoicePostingBuffer.Amount;
                GenJnlLine."Shortcut Dimension 1 Code" := SalesHeader."Shortcut Dimension 1 Code";
                GenJnlLine."Shortcut Dimension 2 Code" := SalesHeader."Shortcut Dimension 2 Code";
                GenJnlLine."Dimension Set ID" := SalesHeader."Dimension Set ID";
                Clear(GenJnlLine."Gen. Posting Type");
                Clear(GenJnlLine."Gen. Bus. Posting Group");
                Clear(GenJnlLine."Gen. Prod. Posting Group");
                Clear(GenJnlLine."VAT Bus. Posting Group");
                Clear(GenJnlLine."VAT Prod. Posting Group");
                Clear(GenJnlLine."Document Type");
                Clear(GenJnlLine."Bal. Account Type");
                Clear(GenJnlLine."Balance Account Id");
                GenJnlLine.Insert();
            end;
        end
        else
            exit;
 
    end;
}
 
GL Entry Extract.
I have the same question (0)
  • Suggested answer
    KP - Solution Architect Technical Profile Picture
    656 on at
    Hi,
     
    Can you please try with this event if it works?
     
     
    Thanks,
    KP
  • Verified answer
    Tech-Lucky Profile Picture
    1,267 Super User 2025 Season 2 on at

    Hi,

    I would recommend not adding the Gen. Journal Line in the middle of the standard posting process, as this is a separate customization. Instead, you should consider using the OnAfterPostSalesDoc trigger of the complete Sales Document and manually call the Gen. Jnl.-Post Line Codeunit to post these lines separately. This approach keeps the customization clean and avoids interference with the standard logic.

    Also, I noticed that the Document No. is missing in your current code — please double-check and ensure it's included.

  • Gerardo Rentería García Profile Picture
    25,157 Most Valuable Professional on at
  • Suggested answer
    YUN ZHU Profile Picture
    95,329 Super User 2025 Season 2 on at
    Hi, hope the following can give you some hints.
    Dynamics 365 Business Central: How to flow/pass custom field values -> From Sales Order (36 Sales Header) to General Ledger Entries (17 G/L Entry)
     
    Thanks.
    ZHU
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,275 Super User 2025 Season 2 on at
  • Verified answer
    One And Only Naveen Profile Picture
    457 on at
    Thank you all for your suggestion's .
     
    And sorry for delayed response.
     
    @Tech-Lucky Sir thank you Sir,
     
    As suggested below method worked for what i needed.
     
     
      [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnAfterPostSalesDoc, '', false, false)]
        procedure PostAdditionalJournalEntry(PreviewMode: Boolean; var CustLedgerEntry: Record "Cust. Ledger Entry"; var SalesHeader: Record "Sales Header")
        var
            GenJournalBatch: Record "Gen. Journal Line";
            GenJournalPostBatch: Codeunit "Gen. Jnl.-Post Batch";
            Customer: Record Customer;
        begin
            Customer.SetRange("No.", CustLedgerEntry."Customer No.");

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans