Tip 2: Recursive functions in Deluge: How to dynamically run a function for a specified number of times.

Tip 2: Recursive functions in Deluge: How to dynamically run a function for a specified number of times.



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.