Ten Things I Hate about You – A Love Letter to Deluge
by David Bayly, Senior Developer at RFDM
Week 1: No Loops in Deluge
For many CRM projects, you will need to do custom scripting for customer use cases. Thankfully, Zoho has an inbuilt scripting language called Deluge. Deluge brands itself as “the programming language for non-programmers”, and this will become very evident once you’ve worked with it. In the following weeks, I’m going to cover 10 things I hate about Deluge, cases where I encountered them, and the solutions I came up with to deal with them. This week, we will focus on the Zoho developers and their bizarre decision not to include support for loops in Deluge.
I’m sure there is some merit to this ideology on paper, but in practice, this is a nightmare. It is not uncommon for people to take issue with this decision, either. When I was researching this article, I found this thread from five years ago that is still active. I highly suggest reading it. It’s filled with devs desperately trying to work around this insane restriction and asking for help, and Zoho telling them that they aren’t thinking looplessly and ignoring the problem. Classic Zoho.
Thankfully, all is not lost. You can work around these limitations, and the thread I mentioned earlier is a great source. There are three possible cases when approaching the problem of loops in Deluge.
Case 1: You need to loop over a map, list or JSON formatted string.
Zoho does have the ability to loop through collection variables, so you are in luck. Zoho can loop through these variables with a for each loop. The documentation is here. For JSON strings you need to turn them into a list variable using the toJSONlist() function, but otherwise the syntax is the same.
Case 2: You have a fixed number of iterations you want to use.
It’s bad practice, but you can create a list with entries equal to the number of iterations you need. You can then iterate through them using a for each loop. With multiple layers of loops, you can get x^y iterations where x is your list size and y is the number of lists you have. This is incredibly inefficient, but it does work.
Case 3: You have a variable/large number of iterations.
I hate to say this, but Deluge is not the tool for you. In the previous version of Creator there was some support for recursive programming within limits. You could reach a recursive depth of 75 levels. In theory you could solve this problem by declaring a list and then adding elements to it until you reach your end condition. This syntactically should work in Deluge but whenever I have tried, I get the ever so helpful error message of “An error occurred while performing this action”. Its also risky in that if you don’t reach your end condition, you will cause an infinite loop.
Sample Code for List Addition Loops in Deluge
I think that the developers at Zoho realized this a few years ago and patched for each loop to throw that error if an item is added to the collection variable.
It makes sense from their perspective. CPU cycles are expensive and trusting a dev not to create an infinite loop is a bad idea.
I expect some comments stating that there are no use cases where the data you are manipulating can’t be expressed in the form of a JSON object/List. Unfortunately, this isn’t quite true. I first realized the pain of loopless programming while working on an integration between a WooCommerce instance and Zoho. The plugin we were using could create a new Sales Order but could not populate the line items section. I used a multiline field to hold the data. The data was in an unformatted string. If I were using any modern language, I would parse the data using regular expressions, and barring that, I could use split and string compare with a for loop to do the same. Since Zoho has no regular expressions aside from match, the task was impossible on the Deluge side. I had some control over the format of the string coming in from WooCommerce, so I ended up having to format it as a properly escaped JSON string. It was a nightmare and wasted an incredible amount of time. When I think back to it, I shudder.
My recommendation is to use an external server and webhooks to accomplish your heavier tasks. I may write a tutorial on how to set up a simple bidirectional connection between a Heroku webserver and Zoho after this series is finished. In all honesty, with all the other issues that Deluge has, this is how you should be handling your development tasks.
I hope some of this was helpful. If you would like to learn more about the solutions we provide, please contact us.