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 loop 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:
- // create a function called DuplicateNtimes
- void DuplicateNtimes(int n)
- {
- //insert the task to duplicate record
- insert into <form>
- [
- <field> = <expression>
- <field> = <expression>
- <field> = <expression>
- ]
- if(n-1 >= 1)
- {
- DuplicateNtimes(n-1);
- }
- }
- 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.