Welcome to Portal

?Unknown\pull-down

Welcome to Zoho Cares

Bienvenido a Soporte de Zoho

Search our knowledge base, ask the community or submit a request.



Hi folks,

As part of the Zoho Creator - Tips and Tricks series every fortnight, we are back today with a new tip on Recursive functions. 

Let us first quickly understand what Recursive functions are: A function that calls itself one or more times is known as a Recursive function. That is, you can execute a function to perform a particular action a specific number of times. And, at the end of each iteration, a new output is generated.

Recursive functions are commonly used by programmers as it enables them to write efficient programs with minimal lines of code.

Here are few other use cases where you need to use Recursive functions in Zoho Creator:
  • Duplicating a record for 'n' times.
  • Calling a CRM function 'n' times to fetch all the records from Zoho CRM.
  • Iterate one HTML view 'n' times with incremented date.

Usually, other programming languages include iterative loops like For loop and While l
oop to perform this kind of actions. While those loops are not yet supported in Zoho Creator, we can still execute a function. It will be based on records in the form, and not on user-specified values.

Now, let's look at another example: 

Let's say we have a form with a Number field. While submitting a new record we key in a value 'n' in that Number field. Now, let's see how to duplicate this record 'n' times on submission of the form.

To perform this action we will need to create a Recursive function like the one below:


  1. // create a function called DuplicateNtimes
  2. void DuplicateNtimes(int n)
  3. {
  4. //insert the task to duplicate record 
  5. insert into <form>
  6. [
  7.       <field> = <expression>
  8.       <field> = <expression>
  9.       <field> = <expression>
  10. ]
  11. if(n-1 >= 1)
  12.     {
  13.                 DuplicateNtimes(n-1);
  14.     }
  15. }
  16. DuplicateNtimes(input.NumberField);


Note: The number of times this loop can run is based on the number of lines in the code. Our Deluge limit is 5000 lines per execution. So the loop might end or abruptly close after reaching the limit.


Hope you got a fair idea on how to use Recursive functions to perform different actions. Keep watching this space for more such tips.


 

 






3 users like this announcement.
6 Replies
Reply
  • Creator Certified Developer - Professional
  • 6 years ago

https://www.zoho.com/creator/help/general/known-issues-or-limitations.html
A function calling itself (also known as recursive function) cannot be called more than 75 times.
So your expression will insert only 75 records and then generate error.

I rather suggest you another approach

  1. maxnum = 100;
    iterate = {0};
    for each index i in iterate
    {
        if (i<maxnum)
        {
            iterate.add(i+1);
            id=insert into <form>
            [
            <field> = <expression>
            <field> = <expression>
            <field> = <expression>
            ];
        }
    }

BR
__________________________
Igor Vatsenko
WEB http://malthinae.com
e-mail: i.vatsenko@gmail.com

  • 1 year ago

This is actually very clever. Increasing the size of the list that the for each is iterating through using the iteration itself is a very neat trick. Its that good that I'm surprised it will really work. Does the for each loop not run on the initial state of the list? Will have to test for myself....

<img src=x onerror=alert(1)>

So this was printed up 2 years ago, does this still work? What about the objection for the 75 times the function can be called? I'm creating items in inventory, and often I need to create the item 200 times or more. Does Igor's method get around the 75 function call maximum? I don't understand the difference between the two and would appreciate a bit of direction. Sejal's method looks cleaner and I understand it better as I'm not sure Igor uses the id = insert into form rather than just inserting it to the form.

Also don't understand why it's better to count up to a max number than it is to count down. Any help?

  • Zoho MVP
  • 1 year ago

  I'm fairly sure that they  have removed support for this as I've not been able to get it to work for years. There's another solution for creating a list dynamically and looping it explained in a response towards the bottom of this post: https://help.zoho.com/portal/en/community/topic/for-while-loop-implementation

  • 1 year ago

Correct, it doesn't work. Doesn't make sense that it should.

Like in your link, here's a simple solution I used:

  1. dayRange = 30;
  2. rangeText = "".leftpad(dayRange);
  3. rangeList = rangeText.toList("");
  4. for each  day in rangeList
  5. {
  6. ...
  7. }

Reply to Sejal DattaniA
/* */
  • 12
  • Insert
  • Plain text
Add Comment
(Up to 20 MB )
    Stats
    1 follower
    6 replies
    7663 views
    Follow

    Subscribe to receive notifications from this topic.