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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Payment method on sales order is not correct when I set it with AL code.

(0) ShareShare
ReportReport
Posted on by 10

Hello!
In our case, a web order comes in via API (v2) and it can only set payment terms, so in parallel I set the payment method based on how the customer paid (gets that info via a custom api).

The problem is if I put e.g. card payment and the order is "invoiced", it should register that it has already been paid.

This does not happen if I set the payment method with AL.
However, it works fine if I select it manually one more time on the order.

So my question is, how can I set the payment method on a sales order with AL and everything will be correct all the way.

The example is when I get data to the custom API

    trigger OnInsert()
    var
        OrderH: Record "Sales Header";

    begin
        OrderH.Get("Sales Document Type"::Order, Rec.bcOrderNr);

        if OrderH."No." <> '' then begin
            OrderH.webOrderNumber := Rec.webOrderNr;
            if Rec.TvingandeFakturatyp <> '' then begin

                if LowerCase(Rec.TvingandeFakturatyp) = 'swish' then begin                  
                    OrderH."Payment Method Code" := 'SWISH';
                    OrderH."Payment Terms Code" := 'FÖSRKOTT';
                end else
                    if LowerCase(Rec.TvingandeFakturatyp) = 'kort' then begin
                        OrderH."Payment Method Code" := 'KORT';
                        OrderH."Payment Terms Code" := 'FÖSRKOTT';
                    end;
            end;
            OrderH.Modify();
        end;

    end;

I have the same question (0)
  • Suggested answer
    Tech-Lucky Profile Picture
    1,288 Moderator on at

    if I understood your question correctly you mean you are getting the validation error while you select the payment method on an invoiced order right?

    so you need the same validation if you are doing the same by your AL code, for that, you need to modify your code as Below :

    trigger OnInsert()
        var
            OrderH: Record "Sales Header";
    
        begin
            OrderH.Get("Sales Document Type"::Order, Rec.bcOrderNr);
    
            if OrderH."No." <> '' then begin
                OrderH.webOrderNumber := Rec.webOrderNr;
                if Rec.TvingandeFakturatyp <> '' then begin
    
                    if LowerCase(Rec.TvingandeFakturatyp) = 'swish' then begin                  
                        OrderH.Validate("Payment Method Code",'SWISH');
                        OrderH.Validate("Payment Terms Code",'FÖSRKOTT');
                    end else
                        if LowerCase(Rec.TvingandeFakturatyp) = 'kort' then begin
                            OrderH.Validate("Payment Method Code",'KORT');
                            OrderH.Validate("Payment Terms Code",'FÖSRKOTT');
                        end;
                end;
                OrderH.Modify();
            end;
    
        end;

    I hope this helps you, please verify my answer by Click YES

  • ITS-David Profile Picture
    10 on at

    Thanks for your answer and I tested it with no new result.

    I probably described my problem poorly.

    If the payment method is set with my code, the value seems correct visually on the order but after I post it and check the
    General Ledger Entries, it is not listed as paid (Document type = Payment).

    If I make an identical order and set the payment method manually, it will appear as paid in the General Ledger Entries.

    With Code:
    pastedimage1683643499144v2.png


    Manually:

    pastedimage1683643457211v1.png

  • Verified answer
    Tech-Lucky Profile Picture
    1,288 Moderator on at

    I am sorry I did not get your question previously,

    Now I have understood and explored the code for the related fields on the Sales order page and table and I did not find anything that was Happening at the page level only, but yes you can try one more thing just change your code and validate the payments term code first and then payment method code as below :

    OrderH.Validate("Payment Terms Code",'FÖSRKOTT');      // First              

    OrderH.Validate("Payment Method Code",'SWISH');   //second

    OrderH.Validate("Payment Terms Code",'FÖSRKOTT');              

    OrderH.Validate("Payment Method Code",'KORT');

    if this also does not help you then you need to debug your code in Both processes to find the difference.

    one more thing if you are using different payment method codes and payment terms code in your manual process then you must compare the master setup as well for SWISH ,FÖSRKOTT etc.

  • Verified answer
    Govinda Kumar Profile Picture
    2,310 Moderator on at

    Hi David,
    I wanted to provide an update regarding the issue, so I have tried updating the Payment Method Code and Payment Terms through AL code, and I did not encounter the same problem.

    pastedimage1683692505812v1.png

    pastedimage1683692782381v3.png

    If the sales order can be posted manually with the same payment method and payment terms in your case then debug the code for both sales orders to compare the G/L entries for payment as suggested by Tech Lucky.
    Regards

  • ITS-David Profile Picture
    10 on at

    Thank you for the help Tech Lucky!

    After more probing the problem was solved as you told me!

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

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 566 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 347 Super User 2026 Season 2

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 327 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans