Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Passing custom field on sales line to G/L Entry

(1) 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
    Yi Yong 1,787 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
    YUN ZHU 77,351 Super User 2025 Season 1 on at
    Passing custom field on sales line to G/L Entry
  • Suggested answer
    Sami Ullah Profile Picture
    Sami Ullah 310 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
    rajivsewsarran 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
    Sami Ullah 310 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
    rajivsewsarran 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
    Hein Kruger 511 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,234 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,994 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans