12 Replies Latest reply on Dec 21, 2012 4:36 AM by comment

    get the value of a field based on variable

    Hudi

      hey,

       

      I'm trying to write a dynamic duplication script that will create a new record and insert the foreign key value of the original field. I originally used the 'duplicate record' script step but it became apparent that waht I needed was a duplicate without the data from the original. Since there are many layouts and tables I would like to write this in a modular fashion so I don't have to use script parameters and a huge script that checks for each one.

       

      Screen shot 2012-12-20 at 12.04.40 PM.png

      Basically, I'm trying to set the $data variable with the field $id. but in $data all i'm getting is the value of $id and not the value of the field with the name $id.

       

      Help...

        • 1. Re: get the value of a field based on variable
          comment

          I am not sure it's a worthwhile endeavor, but do have a look at the GetField() function.

           

           

          ---

          Note that in your example, "Store_id" is hard-coded into the script - which means that renaming the field will break the script.

           

           

          Message was edited by: Michael Horak

          • 2. Re: get the value of a field based on variable
            Hudi

            Thanks Michael.

             

            I'm not sure I understand why it would break the script but the reason its hardcoded is because its the same field name for all tables. The only thing that changes is the table name.

             

            I'll try GetField

            • 3. Re: get the value of a field based on variable
              Hudi

              Jackpot.

               

              Thanks Michael!

              • 4. Re: get the value of a field based on variable
                nickchapin

                What Michael is referring to is that if, at some point in the future, you change the field name to "Store_ids", for example, your script will no longer work as the field name is hard-coded into the script. It's best to avoid hard-coding these kinda things if you can. However, as long as you're aware of the possibility....

                • 5. Re: get the value of a field based on variable
                  Hudi

                  gotcha.

                   

                  thanks, i will definetly keep that in mind.

                  • 6. Re: get the value of a field based on variable

                    I have a few occasions where hard-coding is only viable option.  The comment is readily viewable when you select the field so it is here that I note:  "DO NOT CHANGE FIELD NAME.  Used in script bla bla."

                     

                    I too suggest major restraint in hard-coding anywhere but I have not succeeded 100% of the time myself, hard-coding field name in developer script for dynamically creating records using Allow Creation from multiple tables to one, using single small script by capturing into variable the information, going to the new layout and creating records with the variable which translates to:

                     

                    Set Field By Name [ Get ( LayoutTableName ) & "::Store_ID" ; 'value to set' ]

                     

                    BTW, FM Help shows a strange example for GetField() you may have noticed:

                     

                    •GetField(“FirstName”) & “ ”& & GetField(“LastName”) returns the text string Jane Public.

                     

                    Really?  Couldn't they have come up with a better example than suggesting this is how to concatenate?  I now know why I saw this in a file of a person newer to FM.  :-/

                     

                    Edited to fix the broken 'amp' issue.

                    • 7. Re: get the value of a field based on variable
                      comment

                      I think that using a script parameter in conjunction with the GetFieldName() function should eliminate most occasions for hard-coding.

                       

                       

                      ---

                      P.S. Reusable code is overrated, IMHO.

                      • 8. Re: get the value of a field based on variable

                        Hi Michael,

                         

                        I agree with you in almost-absolute theory! 

                         

                        Michael Horak wrote:

                         

                        I think that using a script parameter in conjunction with the GetFieldName() function should eliminate most occasions for hard-coding.

                         

                        GetFieldName() provides the full table occurrence name and field name but there are times (usually for a developer process such as some data migrations with twists, creating records in single audit table from multiple tables etc) where it is just handy to split the pieces apart and roll your own by collecting only the table occurrence name and adding your own static (gulp) field name to run a process.  Isn't it?  I would be open to discuss specific examples (I would provide) and get ideas how to accomplish it in better ways. I always welcome input and want to learn the absolute best in everything.  However, this isn't my thread ...

                         

                        Michael Horak wrote:

                         

                        P.S. Reusable code is overrated, IMHO.

                         

                        I do not see this thread, nor my use of scripting similarly, as reusable code (but then I have not looked up that definition either) .  It is instead an attempt to create a single concise, fast/safe developer-scripted process to achieve a single objective.  To me Reusable Code would mean multipurpose across purposes, LOL.  From that light, and within a single objective, all optimizations are allowed within script (IMO) as long as it is clearly commented and defined.  Proportions are pretty much everything in life.  ;-)

                         

                        Anyway your cautions are absolutely just and it should be last ditch effort.

                        • 9. Re: get the value of a field based on variable
                          nickchapin

                          Yes, GetFieldName() is a poorly-named function as it's really GetTableOccurrenceAndFieldName.  Here's a cf that takes care of that:

                           

                          // _________________________________________________________________________________

                          /*

                          GetFieldNameOnly ( field )

                           

                           

                           

                           

                          RETURNS:                              ( string )

                          DEPENDENCIES:                    none

                          REV:                                        2012-09-01

                           

                           

                          EXAMPLES:                    GetFieldNameOnly ( tableOne::fieldOne  ) returns: fieldOne

                           

                           

                          NOTES:                              Rather than using FM's native GetFieldName function, which will also return the relationship name, this

                                                                  function returns only the field name. Primarily used in SQL calls to protect the field references from breaking if the field is renamed.

                                                                  The Quote () function "escapes" problematical characters and SQL reserved words, if any; otherwise, no harm done.

                           

                           

                          */

                          // _________________________________________________________________________________

                           

                           

                          // _________________________________________________________________________________

                          Let ( [

                                    ¢field = GetFieldName ( field );

                                    ¢field = Substitute ( ¢field; "::"; ¶ )

                                    ];

                           

                           

                                    Quote ( GetValue ( ¢field; 2 ) )

                           

                           

                          )

                          // _________________________________________________________________________________ END OF LINE

                          • 10. Re: get the value of a field based on variable
                            comment

                            LaRetta wrote:

                             

                            collecting only the table occurrence name and adding your own static (gulp) field name to run a process.  Isn't it?  I would be open to discuss specific examples

                             

                            I would welcome an example, esp. one where - as in the OP - there are many tables with the same foreign key. I have a vague suspicion there is a structural issue behind this need. Data migration and audit logs need not apply; anything is permissible in a one-off, and discussing audit logs would indeed amount to hijacking the thread, I think.

                            • 11. Re: get the value of a field based on variable

                              It would be best to see example from Hudi_M anyway since we are probably talking about different process.  I am willing to put something together but it will be worthless if it misses the mark and it probably will since we do not yet have enough details.  The need for this kind of functionality is pretty rare. :-)

                              • 12. Re: get the value of a field based on variable
                                comment

                                LaRetta wrote:

                                 

                                It would be best to see example from Hudi_M anyway

                                 

                                Indeed.