1 2 Previous Next 15 Replies Latest reply on Mar 25, 2011 4:36 PM by philmodjunk

    Referencing a variable has the same name as active field

    Frinholp

      Title

      Referencing a variable has the same name as active field

      Post

      A tough one this one.

      I have set a layout up so that on load, all fields' content on that layout are loaded into variables. These variables bear the same name as the corresponding field name.

      I now need to work backwards.

      Can anyone think of a way to reference the corresponding variable using the active field name?

      e.g Get (ActiveFieldName) = Field1

      I need to reference $$Field1

      Thanks in advance

      Lee

        • 1. Re: Referencing a variable has the same name as active field
          Frinholp

          Could I achieve this using let and evaluate?

          • 2. Re: Referencing a variable has the same name as active field
            philmodjunk

            Not that I can think of without a very complex chain of If steps that will not evaluate quickly if there are a large number of fields on the layout and the script will be so layout specific, you might as well write the script to reference the fields directly.

            This might be a case where you can define a pair of repeating variables or variables set up as lists of values where one Variable lists the field names and the other variable lists the values in matching order. That way you can look at repetition 1 or value 1 in $$FieldNames to get the name of the field and can look at $$FieldValues in the same way to get the field's value.

            Why do you want to set up such an approach? (What problem does this solve for you?)

            • 3. Re: Referencing a variable has the same name as active field
              Frinholp

              I load the values of all fields into variables of the same name so I can check if the field has been modified.

              i.e not ($$FieldName = Table::FieldName)

              This is because I have an edit record mode and would like the user to be aware that no changes have been made to the record when it has been re-submitted.

              Also I have a self-join to check uniqueness. Fine when creating a new record, not when editing.

              Count ( UniqueTable::FieldName ) > 0  and  Table::FieldName  ≠ $$FieldName

              I want to be able to run the same script on any field that requires unique value.

              Count ( UniqueTable::FieldName ) > 0  and  Table::FieldName - sorted 

              ≠ $$FieldName - trouble

              Thanks Phil

              • 4. Re: Referencing a variable has the same name as active field
                philmodjunk

                I remember your earlier explorations into field validation tests for uniqueness with script triggers. Are you trying to use variables here so that the same validation script can work with many different tables and layouts?

                I don't see how your uniqueness check is intended to work here. Table::FieldName  ≠ $$FieldName doesn't make any sense to me nor do I see how a "unique" table is intended to work here.

                You may be better off using a layout that consists of global fields with a "save" script that uses the data to either update an existing record or creates a new one after all validation tests are passed.

                A different useful trick that I may or may not have suggested to you in an earlier post is to use a script trigger with global variable to capture the original value when the field is entered so that a validation test on the field can be performed when the field is exited.

                Use an OnObjectEnter trigger with TableName::Field as the parameter to run this script:  Set Variable [$$OldValue ; Get ( ScriptParameter ) ]

                Now the exiting script can use $$OldValue to compare to the current value on exit and to restore it to the original value if desired. This way, you don't need to save the value of every field on the layout at once, you can just load one variable, $$OldValue, with the original value of the field currently being edited.

                Here's a uniqueness test that does not require a relationship: Define a value list that lists all values from the field, then use this expression to check and see if the uncommitted value already exists:

                IsEmpty ( FilterValues ( ValueListItems ( Get ( FileName ) ; "value list name" ) ; Table::EditedField ) )

                will be true if the value in the edited field does not yet exist in the table. The value list's name might even be passed as a script parameter or you might use a naming convention that makes the value list name a function of the field's name. The down side, of course, is that you'd need to define a value list for every field where you need to perform a uniqueness test.

                • 5. Re: Referencing a variable has the same name as active field
                  Frinholp

                  I remember your earlier explorations into field validation tests for  uniqueness with script triggers. Are you trying to use variables here so  that the same validation script can work with many different tables and  layouts?

                  Yes

                  I don't see how your uniqueness check is intended to work here.  Table::FieldName  ≠ $$FieldName doesn't make any sense to me nor do I  see how a "unique" table is intended to work here.

                  Count ( UniqueTable::FieldName ) > 0  and  Table::FieldName  ≠ $$FieldName

                  UniqueTable is the self-join. All my tables will use the same notation e.g Customers -> UniqueCustomers, Suppliers -> UniqueSuppliers.

                  I can reference the corresponding active field in the self-join table from the current layout table using "Unique" & Get( LayoutTableName ) & "::" & Get ( ActiveFieldName ) making my script re-usable for any table that has unique fields.

                  My records are not commited until a submit button has been pressed. Therefore, if there are no unique fields with the same value, at the point where this test is being used  Count ( UniqueTable::FieldName ) = 0.

                  If in edit mode and the field has not been modified, at the point of the test taking place Count ( UniqueTable::FieldName ) = 1. This means that I have to check that it has not been modified using Table::FieldName  ≠ $$FieldName otherwise Count ( UniqueTable::FieldName ) > 0 will always be true.

                  Hope that explains it.

                  Thanks

                  Lee

                  • 6. Re: Referencing a variable has the same name as active field
                    philmodjunk

                    Count ( UniqueTable::FieldName ) > 0  and  Table::FieldName  ≠ $$FieldName

                    UniqueTable is the self-join. All my tables will use the same notation e.g Customers -> UniqueCustomers, Suppliers -> UniqueSuppliers.

                    I don't see the purpose of all the fields having their own field name specific variable here.

                    I got this expression to display a value: Count ( GetField ( Get ( ScriptParameter ) ) ). I used GetFieldName ( Table::Field ) as the script parameter. With that approach, I can rename the field in Manage | Database | Fields and the script will still work.

                    If the field has not been modified, you should not have a duplicate value to check for. That error should have been prevented by eariler validation tests on this record or any other in the table.

                    • 7. Re: Referencing a variable has the same name as active field
                      Frinholp

                      Thanks again for your time Phil

                      If the field has not been modified, you should not have a duplicate  value to check for. That error should have been prevented by eariler  validation tests on this record or any other in the table.

                      This is true. What method do you suggest to do this? The method I was using was to load all fields into variables on record load, and compare the current value in the field contents with the value of the variable.

                      Lee

                      • 8. Re: Referencing a variable has the same name as active field
                        philmodjunk

                        I wouldn't load any variables on record load. I'd load variables as needed when a field is entered and/or pass values to it in a script parameter. That has much less overhead, and should avoid a lot of the issues that lead you to start this thread.

                        What method do you suggest to do this?

                        For a uniqueness check, I've suggested an expression already. Either

                        Count ( GetField ( Get ( ScriptParameter ) ) )

                        or

                        IsEmpty ( GetField ( Get ( ScriptParameter ) ) )

                        can be used to check and see if a duplicate value in the related self join TO exists.

                        • 9. Re: Referencing a variable has the same name as active field
                          Frinholp

                          Sorry for all my questions here Phil, I'm still learning. I so glad someone is willing to take time out to explain.

                          I wouldn't load any variables on record load. I'd load variables as  needed when a field is entered and/or pass values to it in a script  parameter. That has much less overhead, and should avoid a lot of the  issues that lead you to start this thread.

                          I'm in total agreement with the overhead issue. And of course I wouldn't have all these variable names that need referencing individually.

                          Now the exiting script can use $$OldValue to compare to the current  value on exit and to restore it to the original value if desired. This  way, you don't need to save the value of every field on the layout at  once, you can just load one variable, $$OldValue, with the original  value of the field currently being edited.

                          I see. if the value hasn't changed on exit, don't bother running all or most of the validation scripts/steps anyway.

                          Count ( GetField ( Get ( ScriptParameter ) ) ). I used GetFieldName ( Table::Field ) as the script parameter

                          What TOC are you passing here? The original table or the self-join? I thought you need to pass the self-join TOC in here, then the expression is evaluated by finding related records i.e those that have the same value for the field in question and then counting how many records match (have that value in that field).

                          IsEmpty ( GetField ( Get ( ScriptParameter ) ) )

                          I'm unsure how this works.

                          I don't see the purpose of all the fields having their own field name specific variable here.

                          I originally did this to detect if none of the fields on the layout had been changed on record commit. How else could I check this? Using one variable on field enter as you suggest, if I set another different variable to true to flag a field on the layout has been changed, (the following may seem overkill) and then the field is changed back to its original state, how would I detemine this without knowing the original state.  

                          Thanks again

                          Lee

                          • 10. Re: Referencing a variable has the same name as active field
                            Frinholp

                            Still unsure how I'm going to detect if there has been no changes made to any fields on the layout without loading all fields into individual variables.

                            This is to allow me to throw a warning that no changes have been made to the record.

                            Cheers

                            Lee

                            • 11. Re: Referencing a variable has the same name as active field
                              philmodjunk

                              Use a single global variable set to false when the record is first accessed for editing.

                              In your validation scripts, set this variable to True if the original value in $oldValue did not match the current value of the field.

                              If any field was changed, the Global variable will have changed to True.

                              • 12. Re: Referencing a variable has the same name as active field
                                Frinholp

                                Using one variable on field enter as you suggest, if I set another  different variable to true to flag a field on the layout has been  changed, (the following may seem overkill) and then the field is changed  back to its original state, how would I detemine this without knowing  the original state. 

                                I thought of using a similar technique as you suggest, maybe this will have to be a limitation as I do want to defer to using only one variable to test all fields.

                                Without being impudent Phil, could you please have a look at the previous post to my last - I am still pondering on which technique to use. I have ruled out value list to check for uniqueness as this would go against what I am trying to achieve here - a script that can be re-used in the simplest way possible.

                                Thanks again

                                Lee

                                • 13. Re: Referencing a variable has the same name as active field
                                  philmodjunk

                                  I've suggested this already in response to that question:

                                  For a uniqueness check, I've suggested an expression already. Either

                                  Count ( GetField ( Get ( ScriptParameter ) ) )

                                  or

                                  IsEmpty ( GetField ( Get ( ScriptParameter ) ) )

                                  can be used to check and see if a duplicate value in the related self join TO exists.

                                  You pass GetfieldName(YourTable::YourFIeld) as the field paremeter.

                                  • 14. Re: Referencing a variable has the same name as active field
                                    Frinholp

                                    Sorry Phil

                                    I don't mean to be a pain.

                                    I was asking about the parameter and which TO to pass as my intentions where not to pass a parameter to reference the field, but rather to just use the currently active field as a reference.

                                    I have been juggling about with this for hours now.

                                    I have tried:

                                    Count ( GetField ( Get(ActiveFieldName) ) )

                                    even

                                    Count ( GetField ( GetFieldName(Evaluate(Get(ActiveFieldName))))) - although I'm pretty sure this is the same thing

                                    Still to no avail.

                                    Again I really do appreciate your time.

                                    If you have retired for the weekend, have a good one.

                                    Lee

                                    1 2 Previous Next