1 2 Previous Next 16 Replies Latest reply on Jul 27, 2016 9:51 PM by BMyers

    Passing a Field Name as a Parameter?

    BMyers

      Quick version of my question:

       

      I have a script attached to a button.  I want to use the same script for more than one field, so I’m trying to pass a parameter that represents a field name.  The goal of the script is to put a Boolean 0/1 in a field so it can be used as a checkbox.  I’m having trouble getting the script to recognize a field name in the parameter box.

       

      Long version:

       

      The goal is to create a simple Boolean checkbox, with the field holding the number 0 or 1.  The box itself is designed to be OSX style, with a empty white box icon for 0 and the OSX blue checkmark icon for 1.  Here’s how I’ve done it.  Click the button and the field toggles between 0 and 1 using a SetField script step:  If 0, then SetField to 1, Else If 1, SetField to 0.  Simple.

       

      The button itself is a two-button button bar.  The first button is the empty box icon and the second button is the checkmark icon.  I used the Hide Object When option on the Inspector to force the empty box icon to hide if the field value is 1, and the checkbox button will hide if the field value is 0.  An interesting behavior of the button bar is that if one button is hidden the other buttons to the right will slide over to the left.  So if the field value is 1, then the empty box icon disappears and the checkbox icon appears and slides to the left.  Thus both icons seem to be located in the same position.

       

      My app has several checkboxes.  This is for a table called Tasks so there are checkboxes to denote status or where the Task appears.  E.g., I have Boolean fields and checkboxes for “Done”, “Calendar” (meaning does it appear on the calendar), “Invoice” (meaning does it appear on an invoice), Critical Date, etc.

       

      Clearly I can create separate scripts for each of the checkboxes.  That’s fine but I can’t leave well enough alone.  The goal now is to create a generalized script and use the script parameter feature to pass the target field. 

       

      I can’t figure out how to do it.  I’ve tried using the Get (ScriptParameter) in the script without luck.  I’ve also tried setting a variable, and inserting the script parameter into the variable, then using the variable in the script. 

       

      One final note: if there is a completely different (and better) method to do a Boolean checkbox with custom icons please let me know. 

       

      Thanks

        • 1. Re: Passing a Field Name as a Parameter?
          user19752

          Script parameter is always a text.

          You can get field contents as

          Evaluate ( "filedName" )

          or

          GetField ( "fieldName" )

          where "fieldName" can be Get ( ScriptParameter ) or any calculation resulted as text of fieldName, it may need to contain tableName:: prefix.

          • 2. Re: Passing a Field Name as a Parameter?
            smith7180

            Pass

            GetFieldName(fieldName)

            as the parameter, then use

            Set Field By Name[Get(ScriptParameter); <calculated value>]

            in the script.

            • 3. Re: Passing a Field Name as a Parameter?
              rrrichie

              If all your script does is toggle the field then why not use the "checkbox set" control and use a value list with only a 1 in it.

               

              That will only show one checkbox and clicking it will toggle between empty and 1.  When testing the field you can use isEmpty to check for 0 for example.

               

              The issue with the script is it won't pass the "activefieldname" cause the field isn't active yet when you press the button.  So although you can use the same script, you will have to "set" the fieldname for each field you use the script.

              • 4. Re: Passing a Field Name as a Parameter?
                beverly

                A couple functions that might help here:

                GetField

                &

                GetFieldName

                 

                the last is what I like when I pass a TEXT representation of the table::fieldName values.

                 

                Get ( ScriptParameter ) will return that text, but to make it a "field", use:

                 

                Set Variable ( $myParam ; Get ( ScriptParameter ) )

                Set Field by Name ( GetFieldName ( $myparam ) ; <<your data here >> )

                 

                Set Field By Name

                 

                Not tested, so study these functions & scripts to see what works for you.

                beverly

                • 5. Re: Passing a Field Name as a Parameter?
                  dtcgnet

                  You can just use a regular field instead of a button bar.

                   

                  Create a field, say "OnOffButtonNumber1". You can make it an auto-enter number field or a text field, it doesn't matter which. The formula you can use is something like:

                  If ( IsEmpty ( OnOffButtonNumber1 ) ; 0 ; 1 )

                  Make sure that "Do not evaluate if all referenced fields are empty" is UNCHECKED. Also make sure the "Do not replace existing value of field (if any)" is UNCHECKED.

                   

                  Assign a value list called "1" to it, with just one custom value, "1".

                   

                  When you create a new record, that field will be empty, so it will receive an auto-entered value of 0. From that point on, if you click the box, it will receive a value of 1. If you click it again, it will receive a value of 0.

                   

                  You won't need any scripts or script triggers at all with this method.

                   

                  Sample file attached.

                  • 6. Re: Passing a Field Name as a Parameter?
                    BMyers

                    Great suggestions folks.  I got slammed with a couple of emergencies today so it will take some time to sort through this, so don't take a slow response as disinterest (It's 11pm now).

                    • 7. Re: Passing a Field Name as a Parameter?
                      BMyers

                      @dtcgnet:  That is a very interesting technique to trigger the 1 or 0 in the field.  I haven't seen that one before.  Perhaps I'm being dense (or perhaps it's 11pm) but what makes the field change?  I don't see how clicking on the field triggers the evaluation of your IF statement.

                       

                      How would I use this with a custom icon?  I see how you got the checkbox in the sample file: using a checkbox set with one choice allows the app to use the standard FMP checkbox (either an X or a checkmark).  That is fine but not optimal.  For design purposes I'd like to use the OSX style blue checkboxes.  How could I do that?

                      • 8. Re: Passing a Field Name as a Parameter?
                        dtcgnet

                        The field changes based on the auto-enter calculation that gets evaluated when you click in the box. You can have the system display an X or a check mark. In FM15 (maybe 14 as well), you can control the color of the box, the color and width of the border, and the color of the X or √ that is displayed.

                         

                        New Record, the field is empty, therefore the auto-enter is calculated so it gets a value of 0.

                        After that, it simply toggles. If clicking removes the checkmark, then the field is empty so it is recalculated to 0. If clicking creates the checkmark, then the field contains 1 so it gets the "false" value of the If statement, which is 1.

                         

                        To use it with a custom icon, I think you'd have to use a script like some of the other posters have suggested. You do get a lot of control over how the checkmark boxes are displayed.

                        1 of 1 people found this helpful
                        • 9. Re: Passing a Field Name as a Parameter?
                          dtcgnet

                          You got me to thinking...

                           

                          Yes, you can do it without a script and with a custom icon. It might be a bit easier than with a script, might not. It would have a low "load weight" because it relies on unstored calcs. The attached version uses 1) a global field, into which you can insert a picture and 2) an unstored calculation that is equal to the global field if OnOffField is 1, and empty if that field is 0. The OnOff field is set to have no fill and no line, and the text is so large it essentially fills the box. Tear the attached file apart and you can see if you'd like the idea.

                          • 10. Re: Passing a Field Name as a Parameter?
                            BMyers

                            dtcgnet:  Thanks.  If you don't mind I'd like to learn bit more about your toggle.  I'm still confused about the mechanics behind an auto enter-calculation for a field using a calculation such as:  If ( IsEmpty ( OnOffField ) ; 0 ; 1 )

                             

                            I can't get my meager mind to wrap around how this creates a toggle.  It would seem that it would evaluate the same way every time.  If there is a zero in the field and clicking the checkbox icon clears the field, then it will evaluate to zero. Clicking it again would clear the field again, then evaluate to zero.  Can you explain the steps of this in detail?

                             

                            The reason I ask is that it is easy to set up a custom icon using the button bar trick I described in my original post using the "Hide Object When" option.  If we can get the button bar to trigger the field you designed, then I think we've found the perfect combination.

                            • 11. Re: Passing a Field Name as a Parameter?
                              dtcgnet

                              I've named the field "OnOffField" in my example, so I'll change it to: "FieldA".

                               

                              When a new record is created, the value in FieldA will be blank. Because of that, it is true that IsEmpty ( FieldA ), and so it will be assigned the value for "This evaluates as True", which is 0. Therefore, all new records will start with FieldA set to 0.

                               

                              So you add the field to your layout, and then you end up clicking on it, next to the box showing the only value in the value list, which is 1. That means that FieldA has content, and therefore it is not empty, and IsEmpty ( FieldA ) evaluates to false and takes on that value (1) as a result. If you click it again, FileMaker will say, "remove the content shown by the box for this value". When it removes the content, then FieldA is empty, so IsEmpty ( FieldA ) is true, and gets the "true" value (0) in the If statement.

                               

                              In the second example file I sent, click on the white box to the lower left of the layout. I've stacked two fields on top of each other. When you click in the box, then that field becomes "1", and the calculated container field sets itself equal to the global container field, resulting in the icon being shown.

                               

                              HTH.

                              1 of 1 people found this helpful
                              • 12. Re: Passing a Field Name as a Parameter?
                                BMyers

                                Oh, I get it now.  Since the value list has only one value, just a 1, the value list toggles between 1 and an empty field.

                                 

                                On your second point, about how to do a custom icon, take a look at my attached sample file.  This is your last example file with a modification.  Your process for creating the custom icon is interesting, but at the price of a bit of complexity. You cleverly placed a checkbox over the icon picture and used left padding to make the checkbox not show; thus the picture appears to toggle.

                                 

                                I've combined your method and mine in the sample file.  I created a custom icon using the two-button bar I described in my original post.  I overlaid your checkbox-without-a-check on top of my button bar.  Now the checkbox icon can appear as any svg icon, designed in any way, and the checkbox icon can be assigned a FMP style so future systemwide style changes are easy.

                                 

                                Another thing I like about your method is how portable it is.  It may be copied to any layout and will work without modification.  It can be copied and used for any other field (different checkbox field on a different table) with only three changed in the Inspector: change the data source, and change the field for the "Hide Object When" option for each button.  Done.

                                 

                                Waddya think?

                                • 13. Re: Passing a Field Name as a Parameter?
                                  dtcgnet

                                  I've attached a modified, modified sample. In FM 15, you can control the color of the box, the outline, and the icon (checkmark or X). So...what you're doing can be accomplished a bit more easily using regular native functionality.

                                   

                                  Check out the attached file. I added a box that turns red with a white checkmark, using native functionality.

                                   

                                  What you're trying to accomplish is pretty nice and got me thinking. For certain things, I can sure see a reason to use a custom icon. It all depends on need, cost to the solution, and simplicity of maintenance.

                                   

                                  Good discussion.

                                  • 14. Re: Passing a Field Name as a Parameter?
                                    BMyers

                                    Thanks.  You're right, there are formatting options for the native checkmark.  I decided to use your idea and have formatted the checkbox with a white background with an OSX blue checkmark (I noted that recent OSX versions and Sierra use a white checkmark on a blue box).  The button bar has its uses in the right situation but your method works so well without the button bar.

                                     

                                    It's amazing that this one thing has burned so much time.  Filemaker needs to create a wiki with a section on "best practices for common design elements."

                                    1 2 Previous Next