2 Replies Latest reply on Oct 29, 2015 7:50 AM by coherentkris

    Script Loop?


      Hello all,


      I am trying to create a script loop that will set 20 date fields with dates.  These fields must skip weekend dates.

      So I have 20 date fields named: gToday, gDate + 1, gDate + 2, g Date + 3... and so on.


      My script reads as follows:


      Set Variable [$Date; Value: Get(CurrentDate)]

      Set Field [Global Fields::gToday; $Date]


          Set Variable [$Day; Value: $Day + 1]

          Exit Loop If [$Day = 20]

          Set Variable [$Name; Value: $Name + 1]

          Set Variable [$Date; Value:Let (day = day of week ($Date) ; $Date + Case (day = 7; 1 ;day = 6; 1 ; 2 ; 0 )) + 1]

          Set Field [$Name; $Date]

      End Loop


      Does not work????? Thoughts???

        • 1. Re: Script Loop?


          There's a few things that are making this script not work:


          1. 1. Your Set Field step probably is actually "set field by name". And that needs a fully qualified field name:
            • Customers::CustomerName  is the actual field name that this script step needs.
          2. 2. In your Set Variable $Name step, you're simply adding numbers together. The first time you set this field, it is nothing + 1 = 1. The 2nd time, it is 1+ 1 = 2.
          3. It is probably a bad idea to use the + in the field name. FileMaker lets you, but it doesn't like that.
            • just call them gDate1, gDate2, and use those in your Set Field script step.


          To remedy this

          1. Create a variable called $Name and set it to "gDate"
          2. Create another variable called $i. This will be your counter.
          3. Create another variable called $Table  = Get (LayoutTableName)
          4. And then in your set field script step, set the field name to $Table&"::"&$Name&$i
            1. This puts the table name, the colons, the name of the field, and appends the number at the end.
          5. Finally at the end of your loop, increase the counter variable by 1
            1. Set field $i    $i + 1
          • 2. Re: Script Loop?

            I would question the need for incrementally numbered fields in the same table. That's usually a sign that the structure can benefit from normalization.


            The issue is with Set Field [$Name; $Date] because $Name will only contain numbers as it is set up to function as an incrementer.


            When you iterate through the loop $Name gets set to 1 and then 1 is added to it at every touch.


            And as J Brown said using any math operator in a field name is permitted by FM but is not a good idea.. hence the FM warning when you save the field definition.