Skip to main content

Notifications

Business Central forum
Suggested answer

Passing custom field on sales line to G/L Entry

Posted on by 97
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.
Categories:
  • Suggested answer
    Yi Yong Profile Picture
    Yi Yong 949 Super User 2024 Season 2 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 71,400 Super User 2024 Season 2 on at
    Passing custom field on sales line to G/L Entry
  • Suggested answer
    Sami Ullah Profile Picture
    Sami Ullah 284 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 97 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 284 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 97 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 499 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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,867 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,173 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans