In this post, I’m going to go over how to use Deluge script with subforms and Zoho Creator. The scenario I have is I’m building an inventory management system where I need to keep track of the moving of items from warehouses to customer orders. For that reason, I’m going to start by creating a form for the warehouse.
I will give it a name, and I will give it some warehouse items using a subform.
I will add a field, which will be the “item name,” I will add another field that I will call “inventory items,” and I will add a number for the number in stock.
While I’m waiting for that to happen, I will create another form for my order.I will add in a field called “customer name” and, of course, this should really be a look-up field to your customer form.
I’m doing a very limited sample subset of what the real app would be. I just want to be able to show you how you would transfer data from one subform to another using Deluge script. We will have “order line items,” we will have the “item name,” and we will have the “quantity purchased.” We would probably also have the amount that you paid for it, but in the interest of time, I’m going to skip that.
What we want to happen is that when the customer makes an order, we should move the item from the warehouse, and reduce the quantity in stock by one. I’m going to make a custom function called “process order.”
It will accept an order and it will scan through each of the line items in the order, find them in the warehouse, and detriment the quantity in stock for the warehouse based on that. We’ll start with this code to iterate over each line item in the order:
We then need to find the relevant item in the warehouse. You could certainly optimise this but I’m going with a simple approach to find the inventory item by looping over all the items in the warehouse.
Of course, you probably need to worry about which warehouse was closest and several other factors as well. We’ll just have one check that the inventory line item actually has stock left.
To summarise the code, we scan through the order, and then we try and find where we can fulfill that particular product from. It might be that one of the products in the order is in Warehouse A, and the second product is in Warehouse B. That’s why we have this for loop here.
Once we’ve found the relevant warehouse that has the item in stock, we decrement the quantity in stock.
Like I said earlier, this is not really an optimal way to do it. This becomes very inefficient if we had 50 different warehouses and each warehouse had 500 products, but for the purpose of our little demo, it will work.
Let me show you what it will look like if we take away two bike helmets from a warehouse’s stock. Click on “execute” and then give it the ID of the order.
When we go back to the warehouse, we should find that the quantity of the bike helmet has gone from three to one.
This works, but it is not a good way to do it. I’ll show you how it should be done. Rather than having these subforms that I created as part of the form, we need to actually change it so that the subform is based on its own form which will allow us to query directly rather than having to iterate over each warehouse’s inventory.
I will make a new form called “inventory item.” It will have the name of the item, it will have the warehouse the item is stored in, and it will have the quantity in stock. Then I will go back to my warehouse form, and I will replace the existing subform with a subform that is based on that independent form.
I will choose an inventory item and I will call it “Inventory Items 2.”
Rather than having two full loops, I will do a query: inventory_item = Inventory_Item[Item_Name == line_item.Item_Name && Quanity_In_Stock > 0];
Just like that, I can delete a whole bunch of code, and also make the code much more efficient because having a for loop inside of a for loop is, generally, a very bad sign. This is a much better way to go.
In general, I prefer the approach I just showed you where you always create a separate form for subforms rather than embedding them inside another form. It makes everything more efficient.
I hope that helps you to have an understanding of how to use subforms in Zoho Creator, and how to use Deluge script with them.