Search code examples
variable-assignmentfilemakerlookup-tables

Is it possible to store a field (not its contents) as a variable in FileMaker?


I'm starting to clean through some old code in a FileMaker database. Someone has the following script in there which allows users to upload a file to a container field. The script determines which field to store the file in based on the Script Parameter.

Set Error Capture [On]
Allow User Abort [Off]

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

If [$ScriptParameter = "Clean"]
    Insert File [Insert; Display content; Never compress; Target:Inspections::cert_Clean]
Else If [$ScriptParameter = "C of C"]
    Insert File [Insert; Display content; Never compress; Target:Inspections::cert_C of C]
Else If [$ScriptParameter = "Hardness"]
    Insert File [Insert; Display content; Never compress; Target:Inspections::cert_Hardness]
...
.
.
End If

It's more or less a lookup table. I would like to pull the actual Insert File function out of the lookup table, and just use a variable that stores the target field. Then have the Insert File get its Target from the variable. Is it possible to store the field itself, rather than its contents, with Set Variable?


Solution

  • A script parameter is always a value, not a field reference.

    The other thing is that the Insert File script step will not let you specify the target field dynamically; you must "bake" the target field into the step.

    What you could do, though, is insert the file into a variable first, then use the Set Field By Name script step to place the inserted file in the target field as determined by the script parameter. But then you would need to pass the target field's name (as Text) as the script parameter - for example, make the parameter:

    GetFieldName ( Target:Inspections::cert_Clean )
    

    for the first field, and so on. Then your script could do simply:

    Insert File [ $insertedFile ]
    Set Field By Name [ Get ( ScriptParameter ); $insertedFile ]
    

    P.S. This step:

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

    does not accomplish anything except wasting CPU cycles and memory. the script parameter itself is already a variable; you can refer to it directly.