Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

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

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;

Categories:
  • ITS-David Profile Picture
    ITS-David 10 on at
    RE: Payment method on sales order is not correct when I set it with AL code.

    Thank you for the help Tech Lucky!

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

  • Verified answer
    Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: Payment method on sales order is not correct when I set it with AL code.

    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

  • Verified answer
    Tech-Lucky Profile Picture
    Tech-Lucky 796 on at
    RE: Payment method on sales order is not correct when I set it with AL code.

    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.

  • ITS-David Profile Picture
    ITS-David 10 on at
    RE: Payment method on sales order is not correct when I set it with AL code.

    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

  • Suggested answer
    Tech-Lucky Profile Picture
    Tech-Lucky 796 on at
    RE: Payment method on sales order is not correct when I set it with AL code.

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,151 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,993 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans