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...
Suggested answer

Passing custom field on sales line to G/L Entry

(2) ShareShare
ReportReport
Posted on by 22
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.
  • Suggested answer
    Yi Yong Profile Picture
    2,141 Super User 2025 Season 1 on at
    Passing custom field on sales line to G/L Entry
    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.
     
  • Suggested answer
    YUN ZHU Profile Picture
    85,338 Super User 2025 Season 1 on at
    Passing custom field on sales line to G/L Entry
  • Suggested answer
    Sami Ullah Profile Picture
    315 Super User 2025 Season 1 on at
    Passing custom field on sales line to G/L Entry

    Hi @rajivsewsarran,

    Please share your code. You are transferring incorrect data to table 17.
    Thanks
  • rajivsewsarran Profile Picture
    22 on at
    Passing custom field on sales line to G/L Entry
    hi Sami,
     
    Thanks for sharing this .
     
    I'm getting this error

  • Suggested answer
    Sami Ullah Profile Picture
    315 Super User 2025 Season 1 on at
    Passing custom field on sales line to G/L Entry

    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
    22 on at
    Passing custom field on sales line to G/L Entry
    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;
    }
     
  • Hein Kruger Profile Picture
    583 on at
    Passing custom field on sales line to G/L Entry
    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.

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2,655

#2
Mansi Soni Profile Picture

Mansi Soni 1,574

#3
YUN ZHU Profile Picture

YUN ZHU 1,453 Super User 2025 Season 1

Featured topics

Product updates

Dynamics 365 release plans