the Otter

Changed Behavior in Variable Definition

5 posts in this topic

As most people here probably know, the Let ( ) function can be used to define a Local variable. As such, it is possible to build a custom function that defines such a variable, and it is further possible to set said variable to a value including itself. An example would be the following custom function, ErrorList, consisting of the following calculation:

Let ( $ErrorList = List ( $ErrorList ; Get ( LastError ) ) ; "" )

If a Set Variable script step sets the same variable as a custom function like the one above, e.g.

Set Variable [ $ErrorList ; Value: ErrorList ]

…the script step will run appropriately, so long as the contradictory variable—in this case, $ErrorList—is not yet defined. However, once this variable has been defined, executing the preceding script step will cause FileMaker 14 (and perhaps other versions) to suffer an Error #1213 and crash the application. The workaround for this behavior is to have the Set Variable script step set a dummy variable, e.g.

Set Variable [ $x ; Value: ErrorList ]

Even if $x is not referenced anywhere, having a script call the ErrorList function passes the variable $ErrorList to the script’s own context, thus allowing its value to be accessed by later steps in the same script (including subsequent calls to the ErrorList function itself).

In FileMaker 15, this behavior has been changed: local variables defined within a custom function are now valid only within the scope of the function itself, including any recursions. While this alleviates the problem of application crashes, it also results in unexpected behavior when scripts written in earlier versions of FileMaker rely on custom functions to set local variables. When migrating to FileMaker 15, each affected script step must be updated to set the target variable explicitly instead of relying on the custom function to do the work. In other words, the code:

Set Variable [ $ErrorList ; Value: ErrorList ]

which proved fatal in FileMaker 14, is now required grammar for FileMaker 15: FileMaker 15 believes that what happens in the function stays in the function, instead returning the result of the calculation to the variable defined in the Set Variable script step. The FileMaker 14 grammar,

Set Variable [ $x ; Value: ErrorList ]

…thus sets $x to the intended value of $ErrorList while leaving the value of $ErrorList as null.

Unfortunately, this cannot work effectively in a mixed-installation environment: the FileMaker 14 grammar leaves FileMaker 15 clients with unintended null values; the FileMaker 15 grammar causes FileMaker 14 to crash. When upgrading all users to FileMaker 15 is not feasible, the best workaround is to use the FileMaker 14 grammar, then once all relevant script steps are complete, check the value of the intended variable (e.g. $ErrorList) and, if empty, set it to the value of the dummy variable (e.g. $x).

 

ErrorTest.fmp12

Edited by the Otter
Updated Demo File
1 person likes this

Share this post


Link to post
Share on other sites
4 hours ago, the Otter said:

In FileMaker 15, this behavior has been changed: local variables defined within a custom function are now valid only within the scope of the function itself, including any recursions. While this alleviates the problem of application crashes, it also results in unexpected behavior when scripts written in earlier versions of FileMaker rely on custom functions to set local variables. When migrating to FileMaker 15, each affected script step must be updated to set the target variable explicitly instead of relying on the custom function to do the work.

I just tested this, and FileMaker 15 custom functions were happy to set local variables that persist outside the function for me. It looks like the ErrorList function in your demo file isn't setting the $Error variable because the custom function chooses not to save error code 0, and the script doesn't trigger any other errors for it to save.

Share this post


Link to post
Share on other sites

Thanks, jbante. Actually, the custom function only ignores error #0 if any error (including 0) was previously recorded. However, you are correct that I have apparently misunderstood the issue: I tried a simpler custom function that sets a variable and it was able to pass that variable to the script.

Despite my misdiagnosis of the problem, there is still a change in behavior: the first version of the script still crashes FileMaker 14, but not 15; and the second version still fails to set $Error to 0 in FileMaker 15, but not 14. I will update the original post once I have more accurately diagnosed the scenario in which this occurs.

Edited by the Otter

Share this post


Link to post
Share on other sites
Let ( [
         #error = Get ( LastError ) ;
         #newError = If ( #error ≠ 0 ; Trim ( #error & " " & scriptStep ) ) ;

It sure looks to me like the function is trying to ignore error code 0, even if there are previously recorded errors. It looks like FileMaker 15 is successfully setting $Error to empty until the first error, and successfully not appending new lines for error code 0. So FileMaker 15 isn't crashing. Great!

Share this post


Link to post
Share on other sites

Yes, thank you. That’s what I get for typing hurriedly. I have fixed my previous comment.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • By Marinos Kangas
      Hello,
      I am new to FileMaker and still trying to find my way around the scripting language.  Even though the scripting language is quite rich, I don't feel as comfortable and flexible as I do in lets say VB.
      To my query now;  I have designed a solution for my company which processes invoices.  These invoice forms input numerical amounts tied on budget codes for each department and in turn are stored in a different table.  As you can see on the image attached, I cannot find a calculation function that will sum all the amounts for each BudgetCode into AmountAllocated.  AmountAssigned is the yearly department budget predefined.
      I will be grateful for any suggestions or ideas.
      Thanks

    • By fm8443
      I am trying to open my (local) FM 14 Pro file [which has an auto-exec "Import Script"] but the script needs a variable file name.
      How do I pass this variable file name into Filemaker at 1) the time of the file open or 2) at the time of running the script (if I switch to manual execution)?
      Is #1 by using the fmp:// syntax from a web browser external to FM?  (or is that syntax just for use when invoked from INSIDE a FM file?)
    • By fm8443
      I have a Layout in Table Mode as (partially) shown in the attachment.
      (Note:  First row will have a Country Code in it - not shown in attachment)
      I want to "fill-down" the last country code in all rows (in the Country Field) UNTIL the country code changes.  Then I want to similarly do it for that one.  Until I reach the end of rows.
      My database will grow from 500 to 10,000 rows of text/number data.  I am uploading in batches from Excel.
      Is this possible?  I've tried various calculations and scripting without much success.  Thanks!
      I'm trying to get something like this:
      COUNTRY
      IT
      IT
      IT
      IT
      IT
      IT
      IT
      IT
      ES
      ES
      ES
      ES
      ES
      AU
      AU
      AU
      AU
      AU
      IN
      IN
      IN
      IN
      .......etc
       

    • By Rook183
      I am sure this is one of those simple ones… that has me bamboozled for nearly 2 days now.
      I need to limit access of my users viewing only a limited set of "Company" records after they log in. The companies that they are allowed to see are listed in each respective user's profile.
      My opening script goes to the user's profile and creates a global variable for each company that they are allowed to view.
      When I go to the "Manage Security > Edit Privilege Sets > Records > Custom Privileges > Limited > Script", and use any of those variables (e.g. $$Company01"), the records table returns no records at all (i.e. as if there were no matches). When I test the script and use text for any one (or several) of those companies by name (e.g. "ACME PTY LTD"), the access rules work perfectly.
      To be clear: The global variables themselves are correct. I know this this because they work in other scripts absolutely perfectly, so the variables DO match the names in the field.
      The script looks like this:
      $$Company = Table Manufacturer or 
      $$Company01 = Table Manufacturer or
      $$Company02 = Table Manufacturer or 
      $$Company03 = Table Manufacturer or 
      $$Company04 = Table Manufacturer or 
      $$Company05 = Table Manufacturer
       
      In every respect, the variable matches the actual text, but I can only imaging that there is a problem with my syntax?
       
      Please advise.
       
      Many thanks
    • By beckham
      Hi can anyone point me in the right direction, I have a database that as a Field populated by a value list, i.e.,: Christmas,Easter,Mothers Day , etc. I want to chart how many records exist of each chosen category. I thought it would be from a summary field using Total of and count but that option isn't available, How do I get filemaker to count the number of uses of the word Christmas from that field , can anyone help please?
      Thanks in advance