11 Replies Latest reply on Jan 17, 2014 8:11 AM by philmodjunk

    Recursive Custom Function

    ChrisJohnston

      Title

      Recursive Custom Function

      Post

           In my solution that is based on this type of relationship (pic) I have a custom function called Infobase(). What it does is let me deduce the parent of a particular Primary Key. I use it to do things like link Information Topics back to found sets.

      http://pctechtv.com/show/cus.png

           So I am imagining that I keep feeding the result of Infobase() back into itself that I can come up with the parent of all parents in a particular topic. I was looking into to recursion in Custom Function and saw some info that said a Custom Function cannot iterate but it can recursively call itself. Assuming that is true I would think that this can be done with my function Infobase(). Eventually the “empty” of the result could insure we are at the highest parent. In my attempt to create a recursive Custom Function I must be doing something wrong?

        • 1. Re: Recursive Custom Function
          philmodjunk

               The actual text of a custom function can be copied and pasted quite easily into Post a New Answer instead of posting a link to a screen shot.

               Try:

               Let ( id = infobase ( anid ) ;
                       If ( IsEmpty ( id ) ; anid ;
                                                   DuParent ( id )
                          ) // if
                      ) let

          • 2. Re: Recursive Custom Function
            ChrisJohnston

                 Works like a charm! and I learned from you again. Why does id not have to be a parameter or declared as variable with "$",  because it is inside the Let?

            • 3. Re: Recursive Custom Function
              philmodjunk

                   it IS a declared variable. There are three kinds of variables. I call them "let" variables, local script variables and global variables and they have either no $, 1 $ or 2 $$ respectively.

                   A "let" variable, what I used, only exists for as long as the Let function evaluates. Unless you need to retain the value for use outside of that context, there's no need to use $ or $$ as the start of the variable name.

              • 4. Re: Recursive Custom Function
                ChrisJohnston

                     Nice! Thanks

                • 5. Re: Recursive Custom Function
                  ChrisJohnston

                       In a procedure like this would it be possible to keep a count of how many times it runs and return it in a variable or just return it in a way it can be recorded?

                  • 6. Re: Recursive Custom Function
                    philmodjunk

                         Let ( [$$count = $$Count + 1

                         is one option, provided that you set $$Count to 0 before each function call.

                          

                    • 7. Re: Recursive Custom Function
                      ChrisJohnston

                      Let me ask the question and stop asking round the question. I have the type of relationship you originally helped me with and it is working extremely well. The one from this Encapsulation and Breadcrumbs (by the way thank you for the great advice). I took the advice and turned it into the “no webs” version and it is the core relationship of the solution. I am trying to find a way without a script to deduce the level the current record is at. I can do I with a script but it seems to have a big demand on the time it take each records to load. When I disable it my records load faster than I can release the mouse click. I had the same issue with scripts that load my bread crumb. When I wrote more calculation (via ExecuteSQL) based mechanism they are fast and what I want. My script to get the level of the current record looks like this(pic).

                       

                      • 8. Re: Recursive Custom Function
                        philmodjunk

                             Well your script is doing a lot of looping to get the result that you want, and a calculation will have to do the same so it may not be much faster if at all, but isn't this level for a given record going to be static? In other words if you have:

                             Info 1 ---> info 2 ----> info 3----> info 4

                             Isn't "info 3" always going to be at level 3 each time that you access it? If so, you can compute the level 1 time and store it in a number field in the record. Even it if is not, you might still be able to use that method if you use scripts to add/remove or change the links and include scripts that update the value for all affected records.

                             That should allow you to access this info with no delays at all except when making changes to your data trees.

                        • 9. Re: Recursive Custom Function
                          ChrisJohnston

                               Exactamundo! Your input is gold every time. I don't need to calculated it, my create topic script for that mater could easily compute it and store it in its field. Thanks

                          • 10. Re: Recursive Custom Function
                            ChrisJohnston

                                 Is this where Auto-Enter Calculated value comes into play? In my case I could say if no foreign key than 1 else “” and let the topic always create it fro there, hmm….

                            • 11. Re: Recursive Custom Function
                              philmodjunk

                                   That would most likely be my first choice for this also.