1 2 Previous Next 16 Replies Latest reply on Jun 14, 2013 5:24 AM by Jason_Farnsworth

    Dynamic Variables

    Jason_Farnsworth

      Is there a way while performing a script you can automatically create and assign a variable?

       

      For instance, look at a field if I like the contents of the field create a variable and save the field value.

       

      Then later on in the same script create assign the dynamic variable to a new record repeat the process until created variables are put into a new field

       

      Jason Farnsworth

      Midland, Tx

        • 1. Re: Dynamic Variables
          StephenWonfor

          Jason

           

          Take a look here.  http://www.supportgroup.com/explore/dynamically-naming-a-variable/

           

          Stephen

           

          "Have you ever noticed that anybody driving slower than you is an idiot, and anyone going faster than you is a maniac?" --- George Carlin

          • 2. Re: Dynamic Variables
            wsvp

            I never tried it, but, I would imagine using the evaluate function, enclosing a Let function to set a variable should work.  You can also create repeating variables also.

            • 3. Re: Dynamic Variables
              Jason_Farnsworth

              Stephen,

               

              This is a very good link on the creating these variables.

               

              What would be the best way to start the calculation with in the script? What function? example SetVariable, SetField, etc. Or does it matter?

               

              Thanks,

               

              Jason Farnsworth

              • 4. Re: Dynamic Variables
                sporobolus

                on 2013-06-12 12:19 Jason_Farnsworth wrote

                 

                What would be the best way to start the calculation with in the script? What function? example SetVariable, SetField, etc. Or does it matter?

                 

                i think your question is what script step do you use to cause the Let()

                calculation to be executed; any script step that can run a calculation will

                work, but i often use the If script step, like this:

                 

                If [ Evaluate ("Let (" & var_name & "=" & var_value & "; 1) ") = 1 ]

                 

                by doing this i can more or less throw away the result of the Let() function

                call, but if i want to check whether the variable setting expression was

                well-formed, i can include an Else block which handles error conditions

                (because Evaluate won't return 1 for malformed expressions)

                 

                not covered in the article Stephen linked is the fact that since you are using

                Evaluate(), any returns in a string will be replaced by spaces; watch out for

                that if you are assigning multi-line text values

                 

                the articles example's are all global variables, but i would use local

                variables where possible

                • 5. Re: Dynamic Variables
                  Jason_Farnsworth

                  Stephen,

                   

                  In looking more at the cycle he is using it appears that it only writes the variable if it is a global variable.

                   

                  I would like to get the fieldname and use it for the variable, Is that possible? It seems that I am getting stuck when I try and turn the variable that I have collected into the variable name.

                   

                  I am also not real clear on the his last step

                  Let (

                     [

                     variableName = $varName ;

                     variableValue = $varValue ;

                     formula = "Let ( variableName = variableValue ; \"\" )"

                     ] ;

                   

                     Evaluate (

                        Substitute (

                           formula ;

                           [ "variableName" ; variableName ] ;

                           [ "variableValue" ; Quote(variableValue) ]

                        )  //close Substitute

                     )  // close Evaluate

                   

                  )  //close Let

                   

                  I am getting lost in the evaluate. When doing these types of calculations it is hard to check the steps at times. I am just not seeing how he is doing it.

                   

                  Thanks,

                   

                  Jason Farnsworth

                  • 6. Re: Dynamic Variables
                    StephenWonfor

                    Jason

                     

                    Same here.  But it works...

                     

                    Stephen

                    • 7. Re: Dynamic Variables
                      DrewTenenholz

                      Jason --

                       

                      I use a slightly different method that involves a few custom functions created by folks much more clever than I.

                       

                      The basic concept I think you are looking for is what can be called a 'Property List' which is a single $var that has content which can look like:

                       

                      Account_Name= Drew

                      Account_Email= dtenenholz@domain.tld

                      Account_NameVisibleGroup=~"D Tenenholz¶drew"

                      ...

                       

                      I can continue to add to this property list throughout the script, then loop though the properties, pulling their values and using Set Field By Name() to populate a new  record.

                       

                      In this way,  you don't need a hundred different $var1 ; $var2 (or $fieldNameA ; $fieldNameB ), you just have one $propertyList with everything in it (up to 2GB of text data).

                       

                       

                      I got my custom functions from http://www.clevelandconsulting.com/support/viewtopic.php?t=59&highlight=getproperty by Shaun Flisakowski around 15 Mar 2004 , but I think that link is long gone.  Shaun is/was a FileMaker engineer and has his own website where they might be found (http://www.spf-15.com/fmExamples/).

                       

                      His functions were called:

                      AddProperty ( propertyList ; propertyName ; propertyValue )

                      GetProperty ( propertyList ; propertyName )

                      RemovePropertyWorker ( prePropertyList ; propertyList ; propertyName )

                      RemoveProperty ( propertyList ; propertyName )

                      ModifyProperty ( propertyList ; propertyName ; propertyValue )

                       

                      These use plenty of Evaluate() and Quote(), but save me the trouble of typing them and tweaking for values with multiple lines.  In short, they just do what they sound like they do, and I pretty much forget the underlying recursive stuff that makes it happen.

                       

                       

                      You could also look into a parallel set of custom functions from Ernest Koe at the Proof Group.  In 2011 he did a presentation in Boston and there were some files included which he asked us to steal from.  He used shorter names (stuff like # and c and other single character function names for ultimate typing ease) but I already had the other set and still use them every day.

                       

                      -- Drew Tenenholz

                       

                       

                      P.S.  One simple architecture I use looks like:

                       

                      Set Variable ( $recordData ; 'something' ) // a single script step to set all of the properties

                      where 'something' looks like:

                       

                      Let ( [

                      propertyList= ""

                      ];

                      AddProperty ( propertyList ; BaseFieldName ( GetFieldName ( Accounts::Account_Name ) ) ; Accounts::Account_Name )

                      & AddProperty ( propertyList ; BaseFieldName ( GetFieldName ( Accounts::Account_Email ) ) ; Accounts::Account_Email )

                      & AddProperty ( propertyList ; BaseFieldName ( GetFieldName ( Accounts::visibleGroup ) ) ; Accounts::visibleGroup )

                      ...

                       

                      ) // end Let

                       

                       

                      which is using the AddProperty() custom function above, and a new Custom Function I wrote BaseFieldName () to get the field name without the part in front of the :: symbol.  I do this so I can be 'contextually independent' and set the correct field based on the TO of the layout where I create the new record.  For example, if I have a separation model system, the TO in the interface table doesn't always match the TO in the data table.  So, having the full TOName::FieldName makes setting the field by name more difficult.

                       

                       

                      To set a record, I get a loop going:

                       

                      Set Variable ( $fieldNames ; GetPropertyNames ( $recordData ) ) // yet another custom function to pull just the property names

                      Loop

                      Set Variable ( $firstField ; GetValue ( $fieldNames ; 1 ) )

                      Set Variable ( $fieldNames ; MiddleValues ( $fieldNames ; 2 ; ValueCount ( $fieldNames ) )

                      Exit Loop If ( IsEmpty ( $firstField )

                      Set Variable ( $fieldValue ; ( GetProperty ( $recordData ; $firstField ) )

                      SetVariable ( $TOName ; Get ( LayoutTableName ) )

                      Set Field By Name ( $TOName & "::" & $firstField ; $fieldValue )

                      // don't  forget to do some error trapping and recording for possible failures

                       

                      End Loop

                      • 8. Re: Dynamic Variables
                        sporobolus

                        on 2013-06-12 14:12 Jason_Farnsworth wrote

                        In looking more at the cycle he is using it appears that it only writes the variable if it is a global variable.

                         

                        that doesn't make sense to me; the code in the article doesn't check variable

                        types; the loop creates a new, sequentially-named global variable, starting

                        with $$day1, at each step in the loop; the variables just happen to be global,

                        they could be local

                         

                        (there is a bug in the code, though; the Exit Loop should have $n not just $)

                         

                         

                        I would like to get the fieldname and use it for the variable, Is that possible?

                         

                        yes; how are you accessing the fields?

                         

                         

                        It seems that I am getting stuck when I try and turn the variable that I have collected into the variable name.

                         

                        show us your code?

                         

                         

                        I am getting lost in the evaluate. When doing these types of calculations it is hard to check the steps at times. I am just not seeing how he is doing it.

                         

                        i think the Substitute() approach in the cited article makes it harder to

                        understand the basic technique; it can be much simpler, as in my previous post

                         

                        but i would heed Drew in case it is a better fit for what you are doing; if

                        not, describing your task more would help

                        • 9. Re: Dynamic Variables
                          Mike Duncan

                          Something like the following is what I use in my navigation frameworks:

                           

                          Evaluate (

                                         "Let ( $$var_name" & $some_other_var & "[" & $number & "] = \"My Value\" ; \"true\" )"

                                         )

                           

                           

                          Probably the biggest issue with this is not necessarily knowing what variables you have set in order to un-set them. So I would consider that before arbitrarily setting too many of these. Closing the file will destroy all variables and free up memory.

                          • 10. Re: Dynamic Variables
                            Jason_Farnsworth

                            Stephen,

                             

                            Haha, well it works don't know how but it does.

                             

                            A couple of notes, what I am trying to do is extract Calculated field values from a record, and then save off the field name and the value of that field. example

                             

                            A quote has

                             

                            a_LAB_BI_01    = Calculated Value

                            a_LAB_BI_02    = Calculated Value

                             

                            Save to Variable

                             

                            Name_1 = a_LAB_BI_01

                            Value_1 = 45

                            Name_2 = a_LAB_BI_02

                            Value_2 = 23

                             

                            Then go to another layout and create a record and setfield with Name_1 and Value_1, then roll to the next record and setfield with Name_2 and Value_2.

                             

                            That said,

                             

                            I created a new layout with all fields from the Quote Table then set a script step to go to the first field and do the saving to Name_1 and Value_1, etc.

                             

                            then go to the next field and do the same saving to Name_2 and Value_2

                             

                            Until I have covered all the fields,

                             

                            Well haha, it contently skips the fields that have a calculated values so it was a bust! Grrr. Through a couple of "if" statements I was filtering off the fields that had no data that I needed.

                             

                            All in all I got (5) out of about 500 fields with data I needed, as the only ones that I got data from where just numbers and not a calculated value.

                             

                            I guess the next step it to study Drew's approach and see if it will apply.

                             

                            I am not real sure if I am making this way harder than it needs to be and would not be surprised if I have dug my self a hole that caved in the deeper I got.

                             

                            At any rate thanks for the advise,

                             

                            Jason Farnsworth

                            • 11. Re: Dynamic Variables
                              DrewTenenholz

                              Jason --

                               

                              The custom functions and property lists aren't magical, they just save on typing all of the Let stuff and free up your mind to think clearly about what you want to do.

                               

                              It looks like you have a quote (quotes_table) which you want to convert to a work order (workOrder_table) with line items (workOrder_lineItems) based on values in the quote.  (It doesn't matter whether the values in the quote are calculated or typed, but it does matter which fields in the work order and line items you want to set.  Those must all be fields that you could type data into by hand (so not calculated or container fields).

                               

                              So, if the process you want is:

                              From Quote -- copy off certain field values

                              Create a new Work Order (capture it's ID for linking)

                              Create several new Work Order Line Items based on several fields from the quote

                               

                              Then my pseudo script for you would be:

                               

                              Go to Layout ( quotes )

                              Set Variable ( $record Data ; myPropertyList )  // explained below

                              Go to Layout ( Work Orders )

                              New Record

                              Set Variable ( $recordData ; Addproperty ( $recordData ; "workdOrderID" ; workOrderID ) )

                              Go to Layout ( Work Order Line Items)

                              1. here's where the real fun begins

                              SetVariable ( $counter ; 1 )

                              Loop

                              Set Variable ( $firstRecord ; GetProperty ( $recordData ; "Record" & $counter1 )

                              SetVariable ( $fieldNames ; GetPropertyNames ( $firstRecord ) )

                              SetVariable ( $firstField ; GetProperty ( $firstRecord ; GetValue ( $fieldNames ; 1 ) )

                              SetVariable ( $remainingFields ; GetProperty ( $firstRecord ; MiddleValues ( $fieldNames ; 2 ; ValueCount ( $fieldNames ) - 1 ) )

                              Loop like I wrote before.....

                               

                              SetVariable ( $counter ; $counter + 1 )

                              End Loop

                               

                              I hope the setting part is sort of explanatory, you just need to loop through the compiled record data you have, setting records and fields as required.  The real magic is in setting up properties that make sense and are useable for the looping process.  The property names can be anything, you simply must choose wisely.

                               

                              If it were me, I'd probably set up a rather complicated-looking (but still single step) Set Variable().  You could just as easily make it 2...n individual Set Variable() steps, maybe like this (with some simplified syntax from what I wrote before):

                               

                              SetVariable ( $record1 ; AddProperty ( $record1 ; "fieldA" ; quotes::fieldA ) )

                              SetVariable ( $record1 ; AddProperty ( $record1 ; "fieldB" ; quotes::fieldB ) )

                              SetVariable ( $record1 ; AddProperty ( $record1 ; "fieldC" ; quotes::fieldC ) )

                               

                              SetVariable ( $record2 ; AddProperty ( $record2 ; "fieldA" ; quotes::fieldA ) )

                              SetVariable ( $record2 ; AddProperty ( $record2 ; "fieldD" ; quotes::fieldD ) )

                              SetVariable ( $record2 ; AddProperty ( $record2 ; "fieldE" ; quotes::fieldE ) )

                               

                              Set Variable ( $recordData ; AddProperty ( $recordData ; "quotes::quote_id" ; quotes::quote_id ) )

                              Set Variable ( $recordData ; AddProperty ( $recordData ; "Record1" ; $record1 ) )

                              Set Variable ( $recordData ; AddProperty ( $recordData ; "Record2" ; $record2 ) )

                               

                               

                              If you look in the data viewer at $recordData, you get a really long text string  that should look like:

                              quotes::quote_id= some_number

                              Record1=~ fieldA=valueA¶fieldB=valueB¶fieldC=valueC

                              Record2=~ fieldA=valueA¶fieldD=valueD¶fieldE=valueE

                               

                              where the ¶ is supposed to be the FileMaker paragraph (or pilcrows) character.

                               

                               

                              But, it doesn't really matter to me what the variable looks like, I just ask for the piece I want:

                               

                              SetVariable ( $firstRecord ; GetProperty ( $recordData ; "Record1" ) ) would be

                              fieldA=valueA

                              fieldB=valueB

                              fieldC=valueC

                               

                              and

                              GetProperty ( $firstRecord ; "fieldA" ) returns valueA.

                               

                              and so on.

                               

                              These property list functions just pack and unpack data into a (text) variable and let you name them how you choose and pull them out by name.  Terribly useful, and you don't really need to know how they do it, just that it is reliable.

                               

                              So, the original question was "Can I dynamically name variables?", and I'm saying that it is slightly easier (with the help from these custom functions) to use a single variable name and dynamically name something called a 'property' within that variable which you can retrieve by name as well.

                               

                              Hope this makes some sense.

                               

                              -- Drew

                              • 12. Re: Dynamic Variables
                                sporobolus

                                on 2013-06-12 15:18 Jason_Farnsworth wrote

                                Well haha, it contently skips the fields that have a calculated values so it was a bust! Grrr. Through a couple of "if" statements I was filtering off the fields that had no data that I needed.

                                 

                                you must be using Go To Next Field; this does what tab does in the UI, skips

                                calcs; i would use the FieldNames() function instead, then GetField() to get

                                the values

                                 

                                Save to Variable

                                 

                                Name_1 = a_LAB_BI_01

                                Value_1 = 45

                                Name_2 = a_LAB_BI_02

                                Value_2 = 23

                                 

                                Then go to another layout and create a record and setfield with Name_1 and Value_1, then roll to the next record and setfield with Name_2 and Value_2.

                                 

                                i thought you wanted to name the variables using the names of the fields … if

                                the above is all you are doing there is no need for dynamic variable names or

                                property lists; you really just need to iterate over the value list

                                FieldNames() returns, and store the values of the fields in a repeating variable

                                 

                                quickie, in shorthand:

                                 

                                set var [$field_names; FieldNames("your table name"; "")]
                                set var [$loop_counter ; 1]
                                loop
                                   set var [$field_values[$loop_counter]; GetField(GetValue($field_names; 
                                $loop_counter))] // note repeating field notation
                                   set var [$loop_counter; $loop_counter + 1]
                                   exit loop if [$loop_counter > ValueCount ($field_names)]
                                end loop
                                

                                 

                                • 13. Re: Dynamic Variables
                                  Jason_Farnsworth

                                  Stephen,

                                   

                                   

                                   

                                  you must be using Go To Next Field; this does what tab does in the UI, skips

                                  calcs; i would use the FieldNames() function instead, then GetField() to get

                                  the values

                                   

                                  When using the FieldNames() function as a variable is gives me a list of all the fieldnames under that layout. They are stored within that one variable as a list. Now that I have them all listed how do you extract them one by one? Is there a function for grabing them one by one?

                                   

                                  Jason Farnsworth

                                  • 14. Re: Dynamic Variables
                                    StephenWonfor

                                    Jason

                                     

                                    ValueCount() and GetValue() are useful in ¶ stacked data

                                     

                                    Stephen

                                     

                                    -


                                     

                                    PHILLIPS  SCREWDRIVER:  Normally used to stab the vacuum seals under lids or for opening old-style paper-and-tin oil cans and splashing oil on your shirt; but can also be used, as the name implies, to strip out Phillips screw heads.  (---Attributed to the list at http://techtalk.parts-express.com/; Also claimed by others...)

                                    1 2 Previous Next