In an earlier article we explored an [isolation technique][isolate] that was specifically designed to work on ID’s and that’s it. There is a lot more that can be done with the new `Set Field By Name` step and in this article we’ll take a look at a slightly more complex but much more versatile search method. This technique will rely on the use of our dictionary functions, so taking a look at this [article][tab-article] is a definite prerequisite.
First off lets go over the basic concept. Essentially, the idea is that with `Set Field By Name` step, we can now pass in the field we want to search as well as the value to search for. So, instead of having to script a bunch of hard coded `Set Fields` we can call one find script, pass in a bunch of find criteria and have that script set the fields we want dynamically. With this new system, you can have one script perform just about any find you need.
Before our script will work, we need a system to pass the name of the search field as well as the value to search. Using our parameter passing technique it’s actually pretty simple to send the information. To pass in a specific field name we can use the `GetFieldName()` in conjunction with `#()`. Here’s the example` #(getfieldname(yourfieldnamehere); criteria)`. So, let’s say you have the NAME field in a table called CUSTOMER and you want to find `Sarah`. Here’s what I need to pass into the script:
#( GetFieldName( CUSTOMER::NAME ) ; “Sarah” )
At this point we’ve determined what we want to search for and where we want to search. Now we need to write a script that will take this information and generate our search. In concept it’s pretty simply. Just loop through any parameters and simply set the specified fields to the value provided. Now, since we are using the`Set Field By Name` script step, be sure that the field name you pass in is the fully qualified field name and that you have the right context before you call the find script. So, if you are on a layout on the CUSTOMER table and you want to search the ID field be sure that the field name you pass in is CUSTOMER::ID. If you pass in ORDERS::ID, you’ll never find anything!
Here’s the actual script:
The first order of business is to take all of the parameters that were passed into the script and stick them into a variable so we can operate on them more easily. What you end up with in the `$dict` variable is the full dictionary of passed in parameters. Now, we enter find mode, enter a loop and iterate through the dictionary values to create our individual criteria.
The loop is a little complicated. It uses a couple different of our custom functions in our previous article. We use `DictFirst` to grab the name of the parameter on the top of the list, then `DictGet` to get the value that corresponds with the name we’ve found. Those two values are then used to `Set Field By Name`. Finally we need to remove that entry from the dictionary. Once we’ve emptied all the entries out of the dictionary, we exit the loop.
>Note: If you pass in the same field multiple times the final value passed in will be the one ultimately used. You’re basically just overriding the value every time you pass in the same field.
To perform the find, we use our tried and true `Safe Find` script and then return the error to make sure the calling script knows what happened.
This system has the potential to save us a tremendous amount of time. Basically, you can construct your finds on the fly by calculating the names of the search fields and the according search value. This find script is relatively simple and we’ll be fleshing it out to allow for multiple requests and omits, but this is a good first step.
>Note: The find script itself does not navigate to any layouts. We’ve designed it around the context independent scripting principles that we here at Six Fried Rice love so much. Before calling the find script, make sure you are in the correct context for the find you are trying to perform.