Script to set fields based on variables

I have a string of fields which I'd like to basically work like toggles. So in the example below, if you click on the DCF field it will set that field to DCF if it's empty or clear it if it's full.

I wrote a script for DCF, and would rather not copy that script for L and C and W and everywhere else I need it, and I feel like there's a simple script or function I can use for this.

I'd love to have a script called "ToggleMe" and pass in the field name (whatever::whatever) and what result to toggle it to (i.e. DCF, L, whatever).

Then I can just add a script to these fields and pass in a script parameter which is their own field name, and what I want to toggle it to. Is this possible? Is there another easier way I'm missing?

image

I think a script receiving both the field name and the value to toggle as you describe should give you what you are looking for.

In itself this should be easy to put together, acknowledging that something easy for one could be somewhat complex to someone else. There could be other approaches, some of them potentially easier, but I think they would deal with some limitations.

You ask if it is possible, am I missing something that makes this more complex than it seems?

1 Like

You may want to rethink things. Why store "DCF" or "L", "C" or "W"? That makes sense if these are different options which are stored in one field. If they are separate fields then you may find it more productive to change the field to a number type and store empty, zero or one.

Making this change would allow you to have a script which looks like this:

# parameter required: a fully qualified field name

Set Field [ by Name ;  Get( ScriptParameter ) ;     not GetAsBoolean( GetField( Get( ScriptParameter) ) ) ]

To ensure that this is always robust, don't hard-code your script parameters, use GetFieldName ( fieldRef )

1 Like

I want to display certain results as they're toggled, so I could make it empty/zero or one, but then I'd have to have "something else" on the screen to display the results. I don't want a checkbox to appear, just a toggleable word.

This is exactly what I was looking for. Thank you!

1 Like

Extra thanks for this. Didn't know this existed!

1 Like

When your data is a boolean, it is extremely easy to use the data as the criterion to show/hide or to conditionally format your on-screen text. As an example, by default all the text could be in a soft grey. Fields that are true could alter the colour to black + hilite yellow.

3 Likes

Gotcha. I do now pass in the option and have been using this script for both boolean fields and non-boolean fields. I have one field which normally would be a dropdown menu, but in this case I want three text fields on the screen and to be able to click on them to toggle between them. You're awesome!