10 Replies Latest reply on May 8, 2012 6:30 PM by Embee

    Script question: first row of portal



      Script question: first row of portal


      Hi fmp's

      I am just getting into scripting in fm and want to set a field to:

      the "PrimaryKey" of the first/top record in "MyDynamicallyFilteredPortal".

      Hope one of you can tell me how to do this?

      - Embee

        • 1. Re: Script question: first row of portal

          I seem to be able to get the right functionality with GetNthRecord:

          Set Field [MyLayoutTable::MyField; GetNthRecord (MyDynamicallyFilteredPortalTable::PrimaryKey;1)]

          is that the right/best way of doing it?

          - Embee

          • 2. Re: Script question: first row of portal

            No, I think you need to drive the focus to the portal (use go to field, or name the portal) and then pick up the data from the Portal Row [First].  The reason I think you method is suspect is because you have to use the relationship only and the records it relates to ignore the filters on the portal.

            • 3. Re: Script question: first row of portal


              You're absolutely right. I made a mistake in calling it MyDynamicallyFilteredPortal... it's "filtered" at the relationship level only. Not with portal filters.

              But i originally wanted to do what you are talking about, and put the focus on the portal (and I still would like to learn) but i can't figure out how to put the script together, so could you maybe elaborate further with a snippet? With the portal object name "MyPortal"?



              • 4. Re: Script question: first row of portal

                Use the name box in the inspector to give your portal an object name.

                Then use this script:

                Go to Object ["objectNameOfPortalHere"]
                Go to Portal Row [first]
                Set Field [YourTable::YourField ; YourPortalTable::Field]
                Commit record

                Commit record removes the focus from the portal. You can leave out that step if you want to leave the focus on this portal row.

                • 5. Re: Script question: first row of portal

                  Thanks, Phil

                  And thanks Sorbsbuster - I'm just so new to scripting that I need to have it spelled out.

                  • 6. Re: Script question: first row of portal

                    Hey Phil, another question to this:

                    When the portal is only filtered at the relationship level, doing only the third script step of your solution: Set Field [YourTable::YourField ; YourPortalTable::Field] does the same thing, since it apparently automatically chooses the first record in the relationship, and there is no portal to remove the focus from.

                    Is that a "good" simplification, or is it just sloppy scripting? I'm guessing sloppy?

                    • 7. Re: Script question: first row of portal

                      I use that method all the time. It is an approach that ignores your portal completely as it relies on the underlying relationship. Any direct reference to a field in a related table will refer to the "first" related record. Set Field [YourTable::YourField ; YourPortalTable::Field] is just one such example. You can setup any number of calculation expressions in conditional formats and calculation fields that obey the same priniciple.

                      What is "first" in a set of related records depends on how you define the relationship. When you double click a relationship line in Manage | Database | Relationships, you can specify a sort order for the related table. The oldest related record will be the first related record if there is no specified sort order. If you specify a sort order, the sort order determines which record is first.

                      • 8. Re: Script question: first row of portal

                        Add a little horsepower to your idea so that when the time comes in the future this will always work. At some point the idea of going to the first record will be replaced by using the second or third or...

                        Just put the button on the portal row. Now when it is clicked it will call a script: 

                        Set variable $_ID to the value in the portal row
                        Do whatever with this variable 

                        • 9. Re: Script question: first row of portal

                          Hi Jack,

                          Sorry the context wasn't explained; what I needed to do here was to set a global field to the ID of the first portal record, from a script trigger placed on a different object than the portal itself (on modify of a pop-up that filters the portal).

                          • 10. Re: Script question: first row of portal


                            Ah, so given the sorting, it's a behavior that one can actually rely on (I didn't dare assume). I think I'll be using that quite a bit, then.

                            Good info - Thanks