11 Replies Latest reply on Aug 20, 2010 4:21 PM by philmodjunk

    Passing a field reference as a script parameter

    Frinholp

      Title

      Passing a field reference as a script parameter

      Post

      I am trying to pass a reference to a field as a script parameter in the format field::table. I have set a local variable $field to hold this field reference. The problem is when I try to use the variable $field in my script, the field reference is being treated as a string. This is not the case when using set field by name - but that expects a string to be entered as the target field and all works fine.

      What function do I use to set the $field variable to hold the value of the field and not the reference string?

      Thanks in advance

      Lee

        • 1. Re: Passing a field reference as a script parameter
          philmodjunk

          Set Variable [ $FieldValue ; GetField ( $FieldName ) ]

          $FieldName will need to hold the name of the table occurrence and the field (Table::Field).

          • 2. Re: Passing a field reference as a script parameter
            Frinholp

            Thanks Phil

            I have another problem now. As I said I'm passing the address of the field into a script as a parameter, $Field. When debugging this has the value TableOC::FieldName as expected and your solution above works fine.

            I am then trying to use the $Field variable (Which is a parameter) to Set Field By Name. That doesn't work. I have also tried using another variable within the script $FieldPath set to GetFieldName ( $Field ), this doesn't work either and $FieldPath shows as a '?' in the data viewer. Do you know what I am doing wrong?

            Also off topic I'm trying to capture mouse clicks outside of fields and buttons. I remember reading a solution not so long ago about you recommending a technique from Mr. Vodka about using a web viewer to capture the clicks. I have read since about people using a large text field that cannot be entered or using a large button assigned to a script that executes nothing. I also read an extreme solution where somebody created an array of single pixel buttons filling the screen area - that seems overkill!

            Could you tell me what the advantage is of the web viewer over large button or text field?

            It would be nice if the filemaker developers could give us the option to turn off save on click outside of objects. I'm sure it's not too much to ask - it plays havoc with my scripts using a self-join to test for uniqueness!

            Cheers

            Lee

            • 3. Re: Passing a field reference as a script parameter
              philmodjunk

              You can join the chorus asking for this by filling out a feature request form: http://www.filemaker.com/company/feature_request.html

              The web viewer seems the quickest to set up.

              As to the indirect field referencing, obviously, some detail isn't set up correctly, but I can't tell what that is from your posts. You'll need to describe things in greater detail and maybe post your script.

              You can print your script to a PDF and use the PDF reader's text tool to copy and paste a script into the forum and you can also copy scripts from a database design report if you have FileMaker Advanced.

              • 4. Re: Passing a field reference as a script parameter
                Frinholp

                Cheers for your time Phil

                Here is my script:

                Medium: Validate Text Unique (tableName; fieldName; field; sjField)

                Set Variable [ $var; Value:DeclareVariables ( Get ( ScriptParameter ) ) ]

                Set Variable [ $fieldValue; Value:GetField ( $field ) ]

                Set Variable [ $fieldPath; Value:GetFieldName ( $field ) ]

                Allow User Abort [ Off ]

                Set Error Capture [ On ]

                Set Variable [ $ValidationComplete; Value:False ]

                If [ IsEmpty ($fieldValue) ]

                Beep

                Show Custom Dialog [ Title: $tableName & " - Validation"; Message: "Please enter a value for " & $fieldName; Buttons:
                “OK” ]

                Exit Script [ ]

                End If

                Set Field By Name [ "$fieldPath"; Trim ($fieldValue) ]

                If [ Count ( $sjField) > 0 ]

                Beep

                Show Custom Dialog [ Title: $tableName & " - Validation"; Message: "The value you have entered for " & $fieldName & " is
                not unique. Please enter a unique value"; Buttons: “OK” ]

                Exit Script [ ]

                End If

                If [ not IsEmpty ( Filter ( $fieldValue ; "0123456789!£$%^&*()-_=+{}[]@#~?><|\`¬" ) ) ]

                Beep

                Show Custom Dialog [ Title: $tableName & " - Validation"; Message: "The " & $fieldName & " field entered as '" &
                $fieldValue & "' contains numeric or special characters. What would you like to do?"; Buttons: “Auto Format”, “Keep The
                Same”, “ Re-Enter Values” ]

                If [ Get(LastMessageChoice) = 2 ]

                Set Variable [ $ValidationComplete; Value:True ]

                End If

                If [ Get(LastMessageChoice) = 3 ]

                Exit Script [ ]

                End If

                End If

                If [ Exact ( $fieldValue ; Upper ( $fieldValue )) ]

                Beep

                Show Custom Dialog [ Title: $tableName & " - Validation"; Message: "The " & $fieldName & "field entered as '" & $fieldValue
                & "' has been entered in uppercase. What would you like to do?"; Buttons: “Auto Format”, “Keep The Same”, “ Re-Enter
                Values” ]

                If [ Get(LastMessageChoice) = 2 ]

                Set Variable [ $ValidationComplete; Value:True ]

                End If

                If [ Get(LastMessageChoice) = 3 ]

                Exit Script [ ]

                End If

                End If

                If [ $ValidationComplete = False and not Get ( LastMessageChoice ) ]

                Set Variable [ $i; Value:1 ]

                Set Variable [ $text; Value:$fieldValue ]

                Set Variable [ $x; Value:2 ]

                Loop

                Set Variable [ $word; Value:LeftWords ( $text ; 1 ) ]

                Set Variable [ $x; Value:2 ]

                Loop

                Set Variable [ $char; Value:Middle ($word ; $x; 1 ) ]

                If [ Exact ( $char ; Upper ( $char ) ) ]

                Beep

                20 August 2010 21:11:42 MayaGallery41020080309-2.fp7 - Validate Text Unique (tableName; fieldName; field; sjField)

                Medium: Validate Text Unique (tableName; fieldName; field; sjField)

                Show Custom Dialog [ Title: $tableName & " - Validation"; Message: "The " & $fieldName & " field entered as
                '" & $fieldValue & "' does not follow the standard naming convention. What would you like to do?";
                Buttons: “Auto Format”, “Keep The Same”, “ Re-Enter Values” ]

                If [ Get(LastMessageChoice) = 2 ]

                Set Variable [ $ValidationComplete; Value:True ]

                End If

                If [ Get(LastMessageChoice) = 3 ]

                Exit Script [ ]

                End If

                End If

                Exit Loop If [ Get(LastMessageChoice) = 2 or Get(LastMessageChoice) = 1 ]

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

                Exit Loop If [ $x > Length ( $word ) ]

                End Loop

                Exit Loop If [ Get(LastMessageChoice) = 2 or Get(LastMessageChoice) = 1 ]

                Set Variable [ $text; Value:RightWords ($fieldValue ; WordCount ( $fieldValue ) - $i ) ]

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

                Exit Loop If [ $i > WordCount ( $fieldValue ) ]

                End Loop

                End If

                If [ $ValidationComplete = False ]

                Set Field By Name [ "$fieldPath"; Proper ($fieldValue) ]

                End If

                 

                Custom function:


                    //  SYNTAX:  DeclareVariables ( ParameterString )
                   //   PURPOSE: Name Value Pairs declaired as local variables
                  //    © 2005, NightWing Enterprises, Melbourne, Australia
                 //     www.nightwing.com.au/FileMaker

                Case(
                  not IsEmpty(ParameterString);
                  Let(
                    [
                      kN = Substitute(ParameterString; [¶; ""]; [" "; ""]; [";"; ""]);
                      kF = Left(kN; 1);
                      kL = Right(kN; 1);
                      k1 = Position(ParameterString; kF; 1; 1);
                      k2 = Position(ParameterString; kL; Length(ParameterString); -1);
                      kP = Middle(ParameterString; k1; k2 - k1 + 1);
                      pS = Substitute(kP; [":¶"; ": "]; [";¶"; "; "]; [¶; "\¶"]);
                      p1 = Position(pS; ": "; 1; 1) + 2;
                      q1 = Left(Trim(Middle(pS; p1; Length(pS) - p1 - 1)); 1);
                      s1 = q1 = "\"";
                      t2 = Choose(s1; "; "; "\"; ");
                      q2 = Left("\""; 1 - s1);
                      p2 = Position(pS & t2; t2; 1; 1);
                      n1 = Left(pS; p1 - 3);
                      c1 = Trim(Middle(pS; p1; p2 - p1 + s1));
                      v1 = Evaluate("Let($" & n1 & " = " & q2 & c1 & q2 & "; \"\")");
                      r1 = Right(pS; Length(pS) - p2 - 1 - s1)
                    ];
                    DeclareVariables ( Trim(r1) )
                  )
                )

                Lee

                • 5. Re: Passing a field reference as a script parameter
                  philmodjunk

                  My eyes are glazing over trying to trace all that through a basic road map would help.

                  What does this line mean: Medium: Validate Text Unique (tableName; fieldName; field; sjField) ?

                  That's not a line I've ever seen in a filemaker script before. Perhaps you have a plug in installed? A comment that's missing the # in your listing?

                  What are you passing as the script parameter here: Set Variable [ $var; Value:DeclareVariables ( Get ( ScriptParameter ) ) ] ?

                  What is the purpose of DeclareVariables ? (what does it do for you?)

                  I may just be missing something here, but I don't see where the variable $field is ever given a value.

                  • 6. Re: Passing a field reference as a script parameter
                    Frinholp

                    Lol. My eyes are sore too.

                    OK, this line here:

                    Medium: Validate Text Unique (tableName; fieldName; field; sjField)

                    Is just the name of the script stored in a Medium folder. I have included those variable names in brackets just to remind me what parameters I need to pass into the script.

                    I use a custom function kindly provided by nightwing called DeclareVariables ( string ). Basically I pass a string into the script,  using the following syntax: 

                    variablename: value; variablename: value; variablename: value; .............etc

                    This string is then used by the custom function DeclareVariables which then creates a variable with the name before the colon and then sets its value to the string after the colon delimited by the semi-colon. I can pass as many variable names and values as I like into the DeclareVariables function and they will be created inside the script.

                    I am passing the following in as parameters:

                    "fieldName: Medium Name; table: Medium; field: ArtworkMedium::MediumName; sjField: UniqueArtworkMedium::MediumName"

                    fieldName is just a string holding the field name I am validating, in this case "Medium Name", for use in the custom dialogs, and table is just a string of the table name I am refering to, again for use in the custom dialogs. 'field' or $field when the variable has been setup holds the TOC & field reference as a string and 'sjField' or $sjField holds the TOC & field of the self-join for the uniqueness test, again all held as a string.

                    Set Variable [ $FieldValue ; GetField ( $FieldName ) ]

                    $FieldName will need to hold the name of the table occurrence and the field (Table::Field).

                    quoted from your post. Instead of using $Fieldname in GetField ( $FieldName) I am passing $Field into GetField.

                    I hope you understand what I am doing, if not don't hesitate to ask.

                    Maybe I am over complicating this and there is a much simpler solution.

                    Thanks again

                    Lee

                    • 7. Re: Passing a field reference as a script parameter
                      philmodjunk

                      An intriguing custom function though such "side effect" programming is not something I'm a big fan of.

                      $Field would be just text that contains the name and table of a given field, thus GetFieldName ( $field ) doesn't make any sense since $field will contain "ArtworkMedium::MediumName" and this IS the field name.

                      I'd use the debugger and data viewer to carefully watch this script execute.

                       

                      • 8. Re: Passing a field reference as a script parameter
                        Frinholp

                        Thanks for your time again Phil

                        It's been a long day and I have made a few silly mistakes, namely when dataviewing and debugging.

                        All of the script runs fine now except the Count on the self-join table. I am holding the the field reference on the self-join in a variable as a string  "UniqueArtworkMedium::MediumName". I now need to suss out some way to extract the reference fom the string and hold it in a variable as a TOC::Field reference so that rather than executing

                        If [ Count ( UniqueArtworkMedium::MediumName) > 0]

                        Instead I can execute

                        If [Count ( $reference ) > 0]

                        BTW the web viewer works superbly

                        Lee

                        • 9. Re: Passing a field reference as a script parameter
                          philmodjunk

                          I think you'll need to use a count function in a field definition--the value of which could then be referenced indirectly, I guess...

                          • 10. Re: Passing a field reference as a script parameter
                            Frinholp

                            Thanks for the pointer.

                            I have created an unstored calculated field cDuplicate that returns a boolean if a field is duplicated.

                            I think I will take the unique value section out of my field validation script as I'm assuming this calculated field would would be better used in a seperate script or custom function for use in scenario's where I am checking for uniqueness on two or more fields; my validation script only validates one field at a time?

                            Thanks

                            Lee

                            • 11. Re: Passing a field reference as a script parameter
                              philmodjunk

                              I've been thinking about this. Properly crafted, you can probably use TocName::Field name in this expression:

                              Evaluate ( "Count ( " & $FieldName " & ")" )

                              To count related records.

                              You can also define the duplicate check relationship to exclude the match to self so that a simple IsEmpty test can be used instead of count.

                              MainTable::SerialID ≠ DupCheck::SerialID AND
                              MainTable::Field = DupCheck::Field