13 Replies Latest reply on Aug 22, 2011 10:08 AM by philmodjunk

    Hard coded tables for my Variables = My enemy!

    Matty_1

      Title

      Hard coded tables for my Variables = My enemy!

      Post

      Hi there,

      I'm currently in the process of setting up a script which pulls a contract number from a field named "ContractNUMBER"  I want to use one script for my button and not have to hardcode the layout name each time I want to pull the data from the field.  I thought a simple Set Variable would do the trick but my code isn't working.  any ideas?

      Here's what I put:

      Name: $CurrentFileName

      Calculation:  Get ( LayoutName ) & "::ContractNUMBER"

      /* Instead of Sales_Contract::ContractNUMBER */

      Repition: 1

      What am I missing?

        • 1. Re: Hard coded tables for my Variables = My enemy!
          philmodjunk

          GetField ( Get ( LayoutName ) & "::ContractNUMBER" ) // to read the data in the field

          and

          Set Field by Name [ Get ( LayoutName ) & "::ContractNUMBER" ; //your expression here ] // to modify the data in the field

          You can also pass the entire table occurrence name and field name as a script parameter:

          GetFieldName ( YourTableOccurrencename::ContractNUMBER ) --which enables you to use this script in a way that allows you to modify table occurrence and/or field names without needing to update your script as the getfieldname function will automatically provide the updated name to your script.

          (And get ( ScriptParameter ) is how you retrieve the parameter's value from inside the script...)

          • 3. Re: Hard coded tables for my Variables = My enemy!
            philmodjunk

            See my lastest update to my response above...

            • 4. Re: Hard coded tables for my Variables = My enemy!
              Matty_1

              Hey Phiil anothier quick one,

              Once again trying not to hardcode table names, I have an If function checking whether the clients prefered invoicing method is email or fax.  The first is what it should look like and the second is what most likely belongs in a function but I'm not sure which one.  Note the table name is "Sales_Contract"

              Contacts_Sales_Contract::PreferedReceiving = "Email"

              Contacts_" & Get (LayoutName) & "::PreferedReceiving" = "Email"

              • 5. Re: Hard coded tables for my Variables = My enemy!
                philmodjunk

                Your expression assumes that the layout name will always be "Sales_Contract". If someone changes your layout name in the future, your script will fail and likely without any warning message to the user. If that's the case, why not just use the first expression?

                That said, you'd want to use:

                GetField ( "Contacts_" & Get (LayoutName) ) & "::PreferedReceiving" = "Email"

                in place of the second expression.

                I'd try to use this expression  instead, if at all possible:

                GetField (Get ( ScriptParameter ) ) = "Email"

                and use GetFieldName ( Contacts_Sales_Contract::PreferedReceiving ) as my script parameter.

                That way, I can change the name of my field or the table occurrence (Such as changing PreferedReceiving to PreferredReceiving Wink ) and I don't have to update my script to keep it working.

                • 6. Re: Hard coded tables for my Variables = My enemy!
                  Matty_1

                  I've never used the Get (ScriptParameter) function, where would I tell it to use GetFieldName ( Contacts_Sales_Contract::PreferedReceiving ) as my script parameter?

                  Further more, if I set that as my script parameter the way it is above am I not hardcoding the layout name?

                  • 7. Re: Hard coded tables for my Variables = My enemy!
                    philmodjunk

                    There are a number of ways you can set up a script to be performed:

                    Button Setup, Script Triggers, Perform Script from another script, Schedules in FileMaker server, File Options (when opening/closing your file)

                    The method I describe can be used with all but the last two. (No parameter box for FileOptions, and Scheduler can only send literal constants.)

                    In all of the others, when you click specify to select the script to be performed, you'll get a dialog that pops us where you can find an optional script parameter box to use to enter this expression.

                    What I am suggesting doesn't hard code the layout name at all. It does not refer to the layout name and you can name the layout anything you want. (The first part of Table::Field, is not the layout's name; it's the layout's TableOccurrence name, the name selected in Show Records From in Layout Setup...)

                    It's also not hard coded to the table occurrence (Table occurrences are the "boxes" you use to define relationships in Manage | Database | Relationships) nor the field name as I have placed that information inside a GetFieldName function. Try it out and see for your self.

                    With what I suggest, you can modify layout, table occurrence and/or field names without having to modify this script parameter.

                    • 8. Re: Hard coded tables for my Variables = My enemy!
                      Matty_1

                      To use the Get (ScriptParameter) function I first have to create the script I want in File|Manage|Custome Functions Correct?

                      • 9. Re: Hard coded tables for my Variables = My enemy!
                        philmodjunk

                        Custom Functions is used to define new calculation functions. These are not scripts.

                        Get (ScriptParameter) should be a standard get function available to you in the Specify Calculation dialog. You'll see the get function listed as "Get (flag)". "ScriptParameter" is one of many "flags" you can use with this function. If you select Get Functions from the View drop down, you'll see all the specific forms of the get function listed so that you can select one by double clicking.

                        Please note that all my posts in this thread assume that you are using a fairly recent version of FileMaker. Older versions, that predate FileMaker 7, do not support script parameters.

                        • 10. Re: Hard coded tables for my Variables = My enemy!
                          Matty_1

                          Hey Phil, Going back on an old thread here.  I still don't know where else to find the parameter box other then in the "Perform script"  With that I am unable to use the script parameter in an if function.

                           

                          Example:  I'm trying to check whether a ticket is open or closed so I created a ticket called "ClosedTicketCheck" Below is the script:

                          If [Get ( ScriptParameter ) = "closed"]

                                Exit Script[]

                          EndIf

                           

                          Now in another script I start by performing the closedticketcheck script.  What I would like is for the script to terminate if the ticket is closed.  Of course setting a variable for a second if function would not work because it will disappear the minute my closedticketcheck script finishes.

                           

                          Is there a trick to using If function with the Perform script functions so I can use the get (script parameter)?

                           

                          • 11. Re: Hard coded tables for my Variables = My enemy!
                            philmodjunk

                            I still don't know where else to find the parameter box other then in the "Perform script"  With that I am unable to use the script parameter in an if function.

                            1. Put a button on your layout. The button setup dialog will appear. Choose perform script and click "Specify". In the next dialog that pops up, there's an option script parameter box in the bottom of this dialog box.
                            2. Right click an object on your layout and select "Script triggers". In the dialog that appears, click a check box to select a new script trigger or select an existing script trigger and click Specify. You'll see the same dialog appear with the same optional script parameter box at the bottom.
                            3. Open a script in the script editor in Manage Scripts. Double click the Perform Script script step to add this step to your script. With this new script step still highlighted, click the specify button at bottom right. Once again, you get the same specify script dialog with the same Optional script parameter box.

                            This box is for specifying what data is sent to the script being performed. You can send it a literal value such as the text "Closed" or it can be a calculation that will be evaluated at the time the perform script event takes place. This calculation can include an If or Case function, though the need for that does not appear to exist here unless I am missing something. Get (ScriptParameter ) is the function you use inside the script being performed to retreive the data that is sent to it as a parameter.

                            Is there a trick to using If function with the Perform script functions so I can use the get (script parameter)?

                            I'm not sure I follow what you want here. What I am describing would work like this for your original request:

                            In button setup, select Perform Script and click Specify to select the script to be performed. Enter an expression in this format into the option script parameter box:  Sales_Contract::ContractNUMBER

                            In the script that will be performed, in any place where you need this number, use Get ( ScriptParameter ) to extract the contractNumber value sent to the script.

                            You are now asking about this code:

                            If [Get ( ScriptParameter ) = "closed"]

                                  Exit Script[]

                            EndIf

                            Your syntax does not contain any errors. If the script, button, or script trigger that performs this script passes "Closed" as the script parameter, this if step will cause the script to exit. If it passes any value other than the text "closed" as the parameter it will not exit the script.

                             

                            • 12. Re: Hard coded tables for my Variables = My enemy!
                              Matty_1

                              Correct, but the above script is called upon via "Perform Script" and is only the first step to several calculations in the parents script called "ClientInvoicing".  What I'm trying to do is setup a button which will verify which is the prefered method for for receiving invoices.  If it's e-mail it will draft and e-mail and attach a PDF, if it's fax it will save the pdf out and prompt the fax program installed.

                              I think I confused you little bit because I'm no longer referring to the contract number issue I was having but more so the Get(ScriptParameter) only.  As you can imagine I have a fairly complex script that checks and executes the proper function according to the prefered receiving method but before doing all that I want to button to do nothing but display a custom dialog if the ticket is closed.  In order to do that (I can only imagine) I will need an If function at the top of the script.  The problem is the exit script function I have in my previous post works of course but exits back to the parents script that launched the "ClosedTicketCheck" script.  I would like to exit everything if the "Sales_Contract::Record_status" field is equal to "Close".

                               

                              My ultimate goal is not to have a single hardcoded script.  Thought this could be a good one to start on because it's complex.

                              !!  I CAN'T ATTACH MY CODING SO I CREATED A NEW POST AT THE FOLLOWING LINK. !!

                              http://forums.filemaker.com/posts/dbb208c40e

                              Please find attached my coding (just figured out I could save it out as a PDF!)  At the top you will see the perform script line that I would like in an if function.  You will also see all sort of hard coded and not so hardcoded lines through out the script.  Eventually I want them all gone.

                               

                              • 13. Re: Hard coded tables for my Variables = My enemy!
                                philmodjunk

                                To "exit everything" use Halt Script instead of Exit Script.

                                For future reference:

                                To post a script to the forum:

                                1. You can upload a screen shot of your script by clicking the edit link to your original message and uploading it there
                                2. You can upload a screen shot to a file share site and post the download link to a new response you post here.
                                3. You can print a script to a PDF, open the PDF and then select and copy the script as text from the opened PDF.
                                4. If You have FileMaker advanced, you can generate a database design report and copy the script as text from there.
                                5. If you paste a text form of the script, you can use the Script Pretty box in the Known Bugs List database to paste a version that is single spaced and indented for a more professional look to your script. (Use the HTML option and paste the text into the HTML editor.)