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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Passing custom field on sales line to G/L Entry

(2) ShareShare
ReportReport
Posted on by 44
HI All,
 
Here's the scenario.
A new field was created on the sales line - which holds tax information. I was able to transfer this amount to the customer ledger and detailed customer ledger entries.
I'm having an issue with being able to pass this value to the G/L entry when the sales invoice is posted.
 
I need it to create a line for the value on the GL Entry.
 
I wrote the proc below but it's not working.
codeunit 50109 WSBCareAPICASITaxRotuine
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)]
    local procedure OnAfterSalesInvoicePost(SalesHeader: Record "Sales Header")
    var
        GLAcc: Record "G/L Account";
        GenJnlLine: Record "G/L Entry";
        SalesLine: Record "Sales Line";
        CSI_TaxAmount: Decimal;
    begin
        SalesLine.SetRange("Document Type", SalesLine."Document Type"::Invoice);
        SalesLine.SetRange("Document No.", SalesHeader."No.");
 
        if SalesLine.FindSet() then
            repeat
                if SalesLine."CSI Tax" <> 0 then
                    CSI_TaxAmount := SalesLine."CSI Tax";
 
                if CSI_TaxAmount <> 0 then begin
                    GenJnlLine.Init();
                    GenJnlLine."G/L Account No." := '2350'; 
                    GenJnlLine."Posting Date" := SalesHeader."Posting Date";
                    GenJnlLine.Description := 'CSI Tax for Sales Invoice ' + SalesHeader."No.";
                    GenJnlLine.Amount := CSI_TaxAmount;
                    GenJnlLine.Insert(true);
 
                     CODEUNIT.Run(Codeunit::"Gen. Jnl.-Post Line", GenJnlLine);
                end;
            until SalesLine.Next() = 0;
    end;
}
 
Any ideas?
 
Side note - this is being done as a make shift solution until the client implements a tax provider next year. I know its not the best... its a work around for now- they need the tax info on the same line - they dont want to pass a new line just for taxes.
  • Hein Kruger Profile Picture
    666 on at
    Most likely there are no more lines to find. 
    OnAfterPostSalesDoc means the 
    Sales Order turned into a Posted Sales Invoice
    When this happens the document and the lines gets deleted after being processed. 
    The Sales Header Table was transferred to the Posted Sales Invoice Header Table.
    The Sales Lines Table was transferred to the Sales Invoice Lines Record. 

    Either use an earlier trigger when the Sales Lines are being processed. 
    Something Like OnRunOnBeforePostSalesLineEndLoop in codeunit 80 "Sales-Post".

    Or use your current eventSubscriber and find the related Posted Sales Invoice Lines.
  • rajivsewsarran Profile Picture
    44 on at
    HIHein,
     
    Thanks for your response!
     
    I adjusted the code to use the sales invoice header and line , but it still doesn't populate.
     
     
    codeunit 50109 WSBCareAPICASITaxRoutine
    {
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Posted Sales Invoice", 'OnAfterPostPostedSalesInvoice', '', false, false)]
        local procedure OnAfterPostedSalesInvoicePost(PostedSalesInvoiceHeader: Record "Posted Sales Invoice Header")
        var
            GLAcc: Record "G/L Account";
            GenJnlLine: Record "G/L Entry";
            PostedSalesInvoiceLine: Record "Posted Sales Invoice Line";
            CSI_TaxAmount: Decimal;
        begin
            PostedSalesInvoiceLine.SetRange("Document Type", PostedSalesInvoiceLine."Document Type"::Invoice);
            PostedSalesInvoiceLine.SetRange("Document No.", PostedSalesInvoiceHeader."No.");
            if PostedSalesInvoiceLine.FindSet() then
                repeat
                    if PostedSalesInvoiceLine."CSI Tax" <> 0 then
                        CSI_TaxAmount := PostedSalesInvoiceLine."CSI Tax";
                    if CSI_TaxAmount <> 0 then begin
                        GenJnlLine.Init();
                        GenJnlLine."G/L Account No." := '2350';
                        GenJnlLine."Posting Date" := PostedSalesInvoiceHeader."Posting Date";
                        GenJnlLine.Description := 'CSI Tax for Posted Sales Invoice ' + PostedSalesInvoiceHeader."No.";
                        GenJnlLine.Amount := CSI_TaxAmount;
                        GenJnlLine.Insert(true);
                        CODEUNIT.Run(Codeunit::"Gen. Jnl.-Post Line", GenJnlLine);
                    end;
                until PostedSalesInvoiceLine.Next() = 0;
        end;
    }
     
  • Suggested answer
    Sami Ullah Profile Picture
    315 Moderator on at

    Hi,

    The OnAfterPostSalesDoc event runs after the Sales Header and Lines are transferred to the Posted Sales documents.

    You should change it to OnAfterPostSalesLines.

    Additionally, the balancing account details are missing in the General Journal.

     

        Permissions = tabledata "G/L Entry" = rimd;
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesLines', '', false, false)]
        local procedure OnAfterPostSalesLines(var SalesHeader: Record "Sales Header"; var SalesShipmentHeader: Record "Sales Shipment Header"; var SalesInvoiceHeader: Record "Sales Invoice Header"; var SalesCrMemoHeader: Record "Sales Cr.Memo Header"; var ReturnReceiptHeader: Record "Return Receipt Header"; WhseShip: Boolean; WhseReceive: Boolean; var SalesLinesProcessed: Boolean; CommitIsSuppressed: Boolean; EverythingInvoiced: Boolean; var TempSalesLineGlobal: Record "Sales Line" temporary)
        var
            GLAcc: Record "G/L Account";
            GenJnlLine: Record "G/L Entry";
            CSI_TaxAmount: Decimal;
        begin
            if TempSalesLineGlobal.FindSet() then
                repeat
                    if TempSalesLineGlobal."CSI Tax" <> 0 then
                        CSI_TaxAmount := TempSalesLineGlobal."CSI Tax";
    
                    if CSI_TaxAmount <> 0 then begin
                        GenJnlLine.Init();
                        GenJnlLine."G/L Account No." := '2350';
                        GenJnlLine."Posting Date" := SalesHeader."Posting Date";
                        GenJnlLine.Description := 'CSI Tax for Sales Invoice ' + SalesHeader."No.";
                        GenJnlLine.Amount := CSI_TaxAmount;
                        GenJnlLine.Insert(true);
                      CODEUNIT.Run(Codeunit::"Gen. Jnl.-Post Line", GenJnlLine);
                    end;
                until TempSalesLineGlobal.Next() = 0;
        end;
     
  • rajivsewsarran Profile Picture
    44 on at
    hi Sami,
     
    Thanks for sharing this .
     
    I'm getting this error

  • Suggested answer
    Sami Ullah Profile Picture
    315 Moderator on at

    Hi @rajivsewsarran,

    Please share your code. You are transferring incorrect data to table 17.
    Thanks
  • Suggested answer
    YUN ZHU Profile Picture
    103,797 Super User 2026 Season 2 on at
  • Suggested answer
    Yi Yong Profile Picture
    2,805 Super User 2026 Season 1 on at
    Hello,
     
    You can refer to this link for the common events to use to transfer custom fields to table.
     
    You will need to transfer to Invoice Posting Buffer -> Invoice Posting Buffer to Gen. Journal Line -> Gen. Journal Line to G/L Entry.
     

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 795 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 561 Super User 2026 Season 2

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 323 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans