# Sum Order Total

**URL:** <https://the.fmsoup.org/t/sum-order-total/3112>\
**Category:** Questions\
**Tags:** scripting\
**Created:** [September 23, 2022, 1:21pm UTC](https://the.fmsoup.org/t/sum-order-total/3112 "2022-09-23T13:21:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steverichter](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steverichter/32/1022_2.png) [@steverichter](https://the.fmsoup.org/u/steverichter)\
**Post date:** [September 23, 2022, 1:21pm UTC](https://the.fmsoup.org/t/sum-order-total/3112/1 "2022-09-23T13:21:38Z")

</div>

I’m trying to set a variable to sum the line items on an order form. I believe the function SUM should work in a calculation. However, the variable $total in the script below calculates only the total for the current record. The variable $sum calculates correctly, but I’m trying to avoid summary fields. Any suggestions?

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/5/58b178da22838050b7d9a842a482a71c853058d0.jpeg)

---

<div class="post-metadata">

**Author:** ![csmu](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/csmu/32/295_2.png) [@csmu](https://the.fmsoup.org/u/csmu)\
**Post date:** [September 23, 2022, 1:29pm UTC](https://the.fmsoup.org/t/sum-order-total/3112/2 "2022-09-23T13:29:45Z")

</div>

Put the lines into a session record. . Master detail for each session. Sum the field via SQL.

---

<div class="post-metadata">

**Author:** ![samfmp](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@samfmp](https://the.fmsoup.org/u/samfmp)\
**Post date:** [September 23, 2022, 1:44pm UTC](https://the.fmsoup.org/t/sum-order-total/3112/3 "2022-09-23T13:44:59Z")

</div>

Could you tell us the relationship of the form? Is it order form to line item( in a portal ) or something like that?

You may use replace field content to a global field which calculate the total of line total.

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [September 23, 2022, 1:47pm UTC](https://the.fmsoup.org/t/sum-order-total/3112/4 "2022-09-23T13:47:45Z")

</div>

Sum works on a set of fields. What it grabs for each field depends on whether the data is in the current table occurrence (TO) or a related TO. For the current TO, it grabs the data only for the current record. For a related TO, it grabs data for all of the related records.

---

<div class="post-metadata">

**Author:** ![steverichter](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steverichter/32/1022_2.png) [@steverichter](https://the.fmsoup.org/u/steverichter)\
**Post date:** [September 23, 2022, 1:50pm UTC](https://the.fmsoup.org/t/sum-order-total/3112/5 "2022-09-23T13:50:39Z")

</div>

It’s not a traditional one-to-many relationship. This is a single table with several fields including qty, srp, and a calculated field for line total.

I think the problem is that the SUM function does not work when defining a variable.

---

<div class="post-metadata">

**Author:** ![mipiano](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/mipiano/32/1074_2.png) [@mipiano](https://the.fmsoup.org/u/mipiano)\
**Post date:** [September 23, 2022, 2:02pm UTC](https://the.fmsoup.org/t/sum-order-total/3112/6 "2022-09-23T14:02:31Z")

</div>

@bdbd gave you the correct answer. It has nothing to do with variables. You’re in one table on one record: Then the scope is the current record. Make a selfjoin and use SUM for the field in the selfjoin TO. Then you will get the correct result.
