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...
Unanswered

How to do some calculations with the previous/subsequent line?

(0) ShareShare
ReportReport
Posted on by

Hey guys,

I would like to do the following:

I have an ordinary Sales Order, which consists of 5 lines.

In the Sales Line table I have added two new fields: Amount & Amount including extra charges.

Ih the first line Amount is 100 and "Amount including extra charges" is 100,too.

It is also 100, because in the first line Amount and "Amount including extra charges" arel always the same.

Now in the second line "Amount including extra charges" is calculated out of "Amount including extra charges" from the first line (so 100) and 70 from Amount from the second line (given by the user)

So "Amount including extra charges"  in the second line will be 170 and that logic should be the same for all other lines.

How can I achieve this?

Thank you ;)

I have the same question (0)
  • Lars Lohndorf-Larsen Profile Picture
    Microsoft Employee on at

    Hi,

    I gave this a try and its actually a bit tricky, depending on the exact requirements. I added these two new fields to table 37:

    Field No. Field Name Data Type
    50100 MyAmount Decimal
    50101 Amount including extra charges Decimal

    And then the logic to calculate the second field:

    MyAmount - OnValidate()
    RunningTotal := 0;
    SalesLineRunningTotal.SETRANGE("Document Type","Document Type");
    SalesLineRunningTotal.SETRANGE("Document No.","Document No.");
    IF SalesLineRunningTotal.FINDFIRST THEN BEGIN
    WHILE SalesLineRunningTotal."Line No." < "Line No." DO BEGIN
    RunningTotal := RunningTotal + SalesLineRunningTotal.MyAmount;
    SalesLineRunningTotal."Amount including extra charges" := RunningTotal;
    SalesLineRunningTotal.MODIFY;
    SalesLineRunningTotal.NEXT;
    END;
    "Amount including extra charges" := RunningTotal + MyAmount;
    END;

    First I tried with a simple REPEAT UNTIL loop, but I found that when trying to modify the current line, it went into a lock. Then there is the situation that when you are inserting a line, you do not know what the "Line No." will be - users could be inserting a line in-between existing lines. This example does not accout for that situation. And what if a user edits the lines and updates MyAmount. So calculating this live while users are editing the order and adding lines, I think is complicated.

    I think the only safe way to implement this, would be to have it as an action that the user can run to update the amounts. Just like "Calculate Invoice Discount". And of course you can run this when posting the order, in case users forget.

  • Community Member Profile Picture
    on at

    Many thanks for your great help ;)

    Did you integrate the code in the Sales Line table or on one of the pages? I just tried the code in the Sales Line table and unfortunately nothing happened ...

  • Lars Lohndorf-Larsen Profile Picture
    Microsoft Employee on at

    Hello,

    I put it on the Sales Line table, on the new field MyAmount OnValidate trigger, so that the code runs when I enter MyAmount. I also added the two fields to page 46 "Sales Order Subform". I hope that shows something?

  • Community Member Profile Picture
    on at

    That's how I did it too, but unfortunately nothing is calculated ...Maybe your code has to be wrapped in a function and should be called in the OnAfterGetRecord trigger?

  • Lars Lohndorf-Larsen Profile Picture
    Microsoft Employee on at

    That's strange. I would put a MESSAGE('I am here.'); on the OnValidate trigger, just to check that it runs at all. Also restart NST in case it is running on Object Cache.

  • Community Member Profile Picture
    on at

    I put a MESSAGE() on the OnValidate trigger and the message is executed, but there is no calculation..."Amount including extra charges" stays in all lines empty.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,055 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,063 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 1,014 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans