Jump to content

Set Field by Name - Using Local Variable


This topic is 2934 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Any idea why $FieldName would not work in a Set Field by Name script step?

$FieldName = Table::FieldName

I've tried to use Evaluate($FieldName), but that didn't work either, resulting in a 102 error - field not found.

Using Table::FieldName directly works, so I know the string is ok.

Link to comment
Share on other sites

I guess my challenge is that the content of $FieldName needs to be generated earlier on in the script. At the moment when the Set Field by Name step happens, Table::FieldName is not the active field. And it should be table independent as well, meaning where ever the user finds himself at the beginning of the script should influence the content of $FieldName.

Hope this makes sense...

I tried GetFieldName ($FieldName), but that failed too.

Edited by Guest
Link to comment
Share on other sites

I am afraid you've lost me: you started with the knowledge of the field's name. The problem was that:

SetVariable [$FieldName ; Table::FieldName]

sets the variable to the CONTENTS of the field.

SetVariable [$FieldName ; "Table::FieldName"]

would have worked - but it would break if the field were renamed.

SetVariable [$FieldName ; GetFieldName ( Table::FieldName )]

sets the variable to the TEXT of the referenced field's qualified name.

However, if you do not have a reference to the field to begin with, then how are you going to identify it?

Link to comment
Share on other sites

I'm sorry if I wasn't clear. Let me try to explain as simply as possible... I'm sorry if I'm changing the terminology, but it will make more sense.

My question is related to an universal find script. I've created a google like search field on every screen, with a script that works entirely layout and table independent.

The essence is that the script reads the table name in the beginning to determine where the user is, and then adds the field name (same in every table) into a local variable.

$SearchField = Get (LayoutTableName) & "::Search Field"

My idea was to use the variable $SearchField in the Set Field by Name script step - as opposed to inserting Get (LayoutTableName) & "::Search Field" - which I realized works just as well in this situation. But wouldn't it be fantastic if a variable containing a field name could be used in such fashion?

Link to comment
Share on other sites

I still don't understand why

Set Field by Name ($SearchField; result)

fail and results in error 102, if $SearchField contains nothing but the result of the calculation

Get (LayoutTableName) & "::Search Field",

for example

TableA::Field1

?

Link to comment
Share on other sites

I created a sample file (see attachment), and IT WORKS. Very strange. I will need to examine my project file for errors.

The populate field script uses the conventional Set Field script step, hard coding the field name, while the reset scripts use the new Set Field by Name step (first with the field name specified directly in the calculation, then using a variable).

SetFieldByName_Variable.fp7.zip

Link to comment
Share on other sites

  • 3 years later...
  • Newbies

It's only a few years late, but since I just had this problem and was looking for a solution, I was frustrated not to find an answer here. I eventually figured it out, so am posting my solution.

 

Initially, I set $fieldvar = table1::field, and then in table2; Set Field By Name [$fieldvar; $value]. This didn't work until I set

$fieldvar = ""table2::" & table1::field & """

Then Set Field By Name [ evaluate ( $fieldvar ); $value]

  • Like 1
Link to comment
Share on other sites

  • 8 months later...

It's only a few years late, but since I just had this problem and was looking for a solution, I was frustrated not to find an answer here. I eventually figured it out, so am posting my solution.

 

Initially, I set $fieldvar = table1::field, and then in table2; Set Field By Name [$fieldvar; $value]. This didn't work until I set

$fieldvar = ""table2::" & table1::field & """

Then Set Field By Name [ evaluate ( $fieldvar ); $value]

 

 

I didn't use your code exactly, but what I needed to see were the tricky & easy-to-omit escaped quotation marks:  " " " .

A big THANK YOU for posting this a few years late -- it's exactly the bit of nitty-gritty I needed to get my script working as it should

Link to comment
Share on other sites

It's only a few years late, but since I just had this problem and was looking for a solution, I was frustrated not to find an answer here. I eventually figured it out, so am posting my solution.

 

Initially, I set $fieldvar = table1::field, and then in table2; Set Field By Name [$fieldvar; $value]. This didn't work until I set

$fieldvar = ""table2::" & table1::field & """

Then Set Field By Name [ evaluate ( $fieldvar ); $value]

 

No, I am afraid that's NOT a good solution at all:

 

First, if table1::field contains "apples", then $fieldvar will be set to the text value of ""table2::apples"". Now, evaluating this expression will return he text value of "table2::apples". So why not set the variable directly to this value ??

 

Much worse, Set Field By Name [ Evaluate ( $fieldvar ) ; "any value" ] will result in an error - unless you do have a field named "apples" in table 2.

Edited by comment
Link to comment
Share on other sites

There are some cases, especially when calling general-purpose scripts, where it saves a lot of repetitive If/Then'nning, to be able to separate out the TO name and the Field name, and to change the TO name in a "Set Field By Name" script step programmatically.

 

I.e. say you're calling a script to drag & drop an image into a table, and you want that to work from multiple layouts with multiple different master TO's & sub-portals.  (NB: these are examples only)

 

If you do:

Set Field [ Dogs_photos::image1 ; <image data> ]

you have to use multiple If / then script steps to accomodate additional tables you often need to work with, i.e. Cats_photos::image1, Horses_photos::image1, Cars_photos::image1 etc.

 

Whereas if you just set the applicable TO name in a variable once, at the top of the script (passed in from a parameter, or perhaps a Get (LayoutTable) ) :

Set Variable [$TO ; Value:"Dogs_photos"] 
Set Field By Name [Evaluate ( """ & $TO & "::image1" & """ ) ; <image data>]

that is a far more generalized solution that eliminates n.. number of If-then decision trees & hand-coded "Set Field [ ]" script steps.

 

Now, if only Filemaker would implement Set Field By Name where ever a Set Field or Go To Target Field is needed, for example in Export Field, Insert File, and umpteen other script steps that require hard-coded field names -- that'd be great.

Link to comment
Share on other sites

There are some cases, especially when calling general-purpose scripts, where it saves a lot of repetitive If/Then'nning, to be able to separate out the TO name and the Field name, and to change the TO name in a "Set Field By Name" script step programmatically.

 

I am not arguing with the need. I'm just saying that the solution proposed above by rainmac suffers from two flaws:

1. it does redundant processing;

2. it doesn't work.

 

I am surprised you haven't noticed the second flaw at least.

Link to comment
Share on other sites

Now, if only Filemaker would implement Set Field By Name where ever a Set Field or Go To Target Field is needed, for example in Export Field, Insert File, and umpteen other script steps that require hard-coded field names -- that'd be great.

 

There are probably workarounds for these - just as there was a workaround for setting a field by name before Filemaker introduced a dedicated step for it. Look at Go to Object, for example.

 

In any case, this kind of "reusable code" is overrated, IMHO. Much too often the "need" arises from inadequate structure. For example, if you have Cats_photos::image1, Horses_photos::image1, Cars_photos::image1 etc., then I would definitely argue that.

 

 

I see your point, yes, he's got some syntax errors & omissions going on there. The key useful part for me was the escaped quotation marks, """, I'd forgotten to add those in my Set Field By Name [ Evaluate ( ) ] script step.

 

If you care to explain what you are trying to do, I'll be happy to show you why this "key useful part" is neither useful nor necessary.

Link to comment
Share on other sites

  • 2 years later...

Here's what works for me:

...
Set Variable [$fieldName; Value: "MyTable::MyField" & $append"] //whatever string that makes up a field name
Set Field By Name [GetFieldName(GetField($fieldName));$newFieldValue]
...


 

Edited by GisMo
Link to comment
Share on other sites

  • 1 month later...
  • Newbies
On 3/21/2013 at 2:37 PM, rainmac said:

It's only a few years late, but since I just had this problem and was looking for a solution, I was frustrated not to find an answer here. I eventually figured it out, so am posting my solution.

 

Initially, I set $fieldvar = table1::field, and then in table2; Set Field By Name [$fieldvar; $value]. This didn't work until I set

$fieldvar = ""table2::" & table1::field & """

Then Set Field By Name [ evaluate ( $fieldvar ); $value]

Took a few tries, this but worked perfectly for me as well.   I was starting to sweat it since I didn't think FM could pull it off, but this was just the trick.  Thanks for posting!

Link to comment
Share on other sites

This topic is 2934 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.