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

Adjust sales order VAT amount programmatically

(0) ShareShare
ReportReport
Posted on by 14

Hi

We are creating sales orders in Business Central from different sales channels (such as Amazon, eBay, etc.) using the API and some customer AL code. Due to different tax rounding methods on the channels we are getting discrepancies in Business Central for the VAT amount, which we are manually adjusting using Order > Statistics > Invoicing > No. of VAT Lines > VAT Amount.

This is a real pain so am looking for a way of automating this, either by overriding the VAT amount when the sales order is created, or by programmatically adjust the order statistics.  Our main problem is a lack of understanding of the order statistics to know what table/codeunit to use.

If anyone has had a similar experience and has come up with a solution please let us know?  Thanks

  • Suggested answer
    JAngle Profile Picture
    159 on at

    The method you're thinking of might be too tricky. From memory I'm sure that page calculates a bunch of variables and uses a temp table.

    Couple of alternatives. Add a g/l account line for a full vat account. Make it have a minus qty. Vat return wise this will be fine as the vat is consolidated at the end of the quarter anyway.

    Don't put the data straight into the sales line table. Put it in a bespoke table and moved it across in BC. At that point of moving it you could include an additional check on the VAT amount.

  • Alan Shurey Profile Picture
    14 on at

    Thanks Josh for the reply.  You are right about the order statistics code being a bit tricky (not sure I want to go there).

    The alternative of adding a G/L account line sounds promising.  My only concern is whether the VAT statement will still pick up the G/L in order to process our HMRC return.  I'll investigate this tomorrow.

    Thanks, Al

  • Samantha73 Profile Picture
    3,390 on at

    I'm still having the issue where I cannot overwrite VAT amounts via an API to match Amazon or any web store. Has anyone found a proper solution to adjust VAT programmatically to overcome rounding differences

  • Suggested answer
    Alan Shurey Profile Picture
    14 on at

    Hi Everyone

    I ended up creating a code unit that could be called after a order was created to update the VAT amount.  I do not have a lot of experience writing AL code so it may not be the best, however, it has resolved the majority of our rounding issues to the point where this is no longer a significant issue for us. We also introduced rounding tolerances for our payment reconciliation that also helped.

    Not sure if this is correct answer, however please feel free to give it a try:

    codeunit 50107 "Sales Order VAT"
    {
        procedure UpdateVAT(orderNumber: Text; newVATAmount: Decimal) RetVal: Text;
        var
            salesHeader: Record "Sales Header";
            salesLine: Record "Sales Line";
            salesLineUpdate: Record "Sales Line";
            vatAmountLine: Record "VAT Amount Line";
            vatDifference: Decimal;
            releaseSalesDocument: Codeunit "Release Sales Document";
            salesCalcDiscountByType: Codeunit "Sales - Calc Discount By Type";
        begin
            // fetch sales header
            salesHeader.SetCurrentKey("No.");
            salesHeader.SetFilter("No.", orderNumber);
            if salesHeader.FindFirst() then begin

                // fetch first sales line
                salesLine.SetRange("Document No.", salesHeader."No.");
                salesLine.SetRange("Document Type", salesHeader."Document Type");
                salesLine.SetFilter(Type, '>0');
                salesLine.SetFilter(Quantity, '>0');
                if salesLine.FindFirst() then begin
                    // calc VAT amount lines        
                    salesLine.CalcVATAmountLines(0, salesHeader, salesLine, vatAmountLine);

                    // update VAT amount lines
                    vatDifference := newVATAmount - vatAmountLine."VAT Amount";
                    if vatDifference <> 0 then begin
                        vatAmountLine."VAT Amount" += vatDifference;
                        vatAmountLine."VAT Difference" := vatDifference;
                        if salesHeader."Prices Including VAT" then
                            vatAmountLine."VAT Base" := vatAmountLine."Amount Including VAT" - vatAmountLine."VAT Amount"
                        else
                            vatAmountLine."Amount Including VAT" := vatAmountLine."VAT Amount" + vatAmountLine."VAT Base";
                        vatAmountLine.Modify();

                        // update sales line
                        salesLine.Amount := vatAmountLine.GetTotalLineAmount(true, salesHeader."Currency Code");
                        salesLine."VAT Base Amount" := newVATAmount;
                        salesLine."VAT Difference" := vatDifference;
                        salesLine."Amount Including VAT" := vatAmountLine.GetTotalAmountInclVAT() + salesLine."Inv. Discount Amount";
                        if not salesHeader."Prices Including VAT" and (salesLine.Amount > 0) and (salesLine.Amount < salesLine."Prepmt. Line Amount") then
                            salesLine."Prepmt. Line Amount" := salesLine.Amount;
                        if salesHeader."Prices Including VAT" and (salesLine."Amount Including VAT" > 0) and (salesLine."Amount Including VAT" < salesLine."Prepmt. Line Amount") then
                            salesLine."Prepmt. Line Amount" := salesLine."Amount Including VAT";
                        salesLine.Modify();

                        // save changes
                        salesLineUpdate.UpdateVATOnLines(1, salesHeader, salesLine, vatAmountLine);
                        releaseSalesDocument.CalcAndUpdateVATOnLines(salesHeader, salesLine);
                        salesCalcDiscountByType.ResetRecalculateInvoiceDisc(salesHeader)
                    end;

                    RetVal := 'VAT updated successfully, VAT (To ' + Format(newVATAmount) + ', Diff ' + Format(vatDifference) + ')';
                end;
            end;
        end;
    }
  • Samantha73 Profile Picture
    3,390 on at

    This is super helpful ..I'll give it a try

  • Suggested answer
    Community Member Profile Picture
    on at

    Alan,

    Adding to others who replied... If you are working with eCommerce sites I would look at implementing PMX e-Commerce Integration:

    appsource.microsoft.com/.../PUBID.focubizgmbh1602450433405|AID.pmxim|PAPPID.9ac9dea3-8882-43ef-923a-cb05ea261ff2

    This would eliminate custom code and make your processing easier.

    Thanks,

    Steve

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 728 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 545 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 328 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans