# Local $variable inside a Single Step button calculation

**URL:** <https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982>\
**Category:** Questions\
**Tags:** calculations\
**Created:** [February 16, 2024, 4:36pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982 "2024-02-16T16:36:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xochi](https://avatars.discourse-cdn.com/v4/letter/x/0ea827/32.png) [@xochi](https://the.fmsoup.org/u/xochi)\
**Post date:** [February 16, 2024, 4:36pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/1 "2024-02-16T16:36:57Z")

</div>

I think I've found a bug in Replace Field Contents where there's a difference between a Button that runs a script vs. a button that has a single script step.

I'd like another pair of eyes on it.

The goal is to take the current found set, and make a list of the record serial numbers, using this calculation:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/c/c19e54fe35a4cee9b812650b29eb4a25269ed290.png)

What this does:

- for each record in the found set
- add the field named "ID" to the list stored in $L
- if the record number = the found count, then we are done and can store the result in the global field

When this is part of a Script, it works normally - the list has 192 values, and the calculated field automatically updates and show a count of 192 values.  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/c/caebd2a79ffe69fd14bdf45fe293d7b48e1ec4c9.png)  
You can click the button repeatedly and it always gives you the same list of 192 values as expected.

However, when this RFC is run from a button which has a single step...

- the first time you run it, the values are correct (192 values). However, the ListCount field does not update (so you need to click the "Refresh" button)
- thereafter, each time you click the Button, the list of values increases in size by 192:  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/3/391bf5f130225d4956cdc2451a7851791b083b94.png)
- closing and re-opening the file resets the behavior, so it starts again at 192, then increases in size each time.

Details:

- I'm seeing this in FileMaker Pro 18 through 20 on macOS running locally. I have not tried it in FMS yet.

Here's the sample file:  
[RFCbug-v1.fmp12.zip](https://the.fmsoup.org/uploads/short-url/rrQ9FUfZFHKyq17dpKNOqTH8z3F.zip) (248.4 KB)

---

<div class="post-metadata">

**Author:** ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)\
**Post date:** [February 16, 2024, 5:10pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/2 "2024-02-16T17:10:25Z")

</div>

Great observation. I think it is correct behavior since the scope of your $L local var is within the script (which clears out when the script terminates) in one case and in the other within the layout of the button since the single step button "script step" is not a script. Hope I explained well ..

---

<div class="post-metadata">

**Author:** ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)\
**Post date:** [February 16, 2024, 5:17pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/3 "2024-02-16T17:17:48Z")

</div>

BTW you could fix it and make it work as desired in the button by adding another case/if to your let statement: Let ( $L = if ( get ( RecordNumber ) = 1 ; Table::ID ; List ( ... ) ; ... )

---

<div class="post-metadata">

**Author:** ![xochi](https://avatars.discourse-cdn.com/v4/letter/x/0ea827/32.png) [@xochi](https://the.fmsoup.org/u/xochi)\
**Post date:** [February 16, 2024, 5:25pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/4 "2024-02-16T17:25:55Z")

</div>

I think you are correct, but it's even weirder. The $L variable is not only persistent on the layout, it also keeps its value on other layouts and TOs as well.

So the real issue appears to be something more general:

- a local $Variable set in a Button that executes a Single Step, the $Variable will be available to all Single Step buttons across the entire database.

I'm sure I don't like it. Is it a bug? Hmm..

---

<div class="post-metadata">

**Author:** ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)\
**Post date:** [February 16, 2024, 5:59pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/5 "2024-02-16T17:59:22Z")

</div>

since it isn't properly documented at least AFAIK it might get out of control to grasp entire behavior in every foreseeable contexts and it might slightly alter its behavior on revised versions.

---

<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:** [February 16, 2024, 6:03pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/6 "2024-02-16T18:03:12Z")

</div>

This is old news. $variables are designed to be scoped to a script. Their behaviour outside of scripts is undocumented, poorly understood and discouraged.

---

<div class="post-metadata">

**Author:** ![FileKraft](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/filekraft/32/1183_2.png) [@FileKraft](https://the.fmsoup.org/u/FileKraft)\
**Post date:** [February 16, 2024, 6:39pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/7 "2024-02-16T18:39:52Z")

</div>

Where I am with @xochi is that a one script step button appears to some as a mini script and therefor somehow expected behavior to be consistent..

---

<div class="post-metadata">

**Author:** ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)\
**Post date:** [February 18, 2024, 6:21pm UTC](https://the.fmsoup.org/t/local-variable-inside-a-single-step-button-calculation/3982/8 "2024-02-18T18:21:25Z")

</div>

> [@Local variables defined in layout objects live long and travel widely](https://the.fmsoup.org/t/local-variables-defined-in-layout-objects-live-long-and-travel-widely/2870):
>
> [The FileMaker documentation says](https://help.claris.com/en/pro-help/content/using-variables.html?Highlight=variables): When you define a variable, you can specify its scope by adding a prefix before the variable name. Scope Prefix Example Availability function None variableName Only within the function where it is defined local $ $variableName Only within the script where it is defined (not available to sub-scripts); cleared when the script ends global $$ $variableName Anywhere within the file where it is defined; cleared when the file is closed For local (sin…
