Form
|
Form Link Name
|
Field Name
|
Field Link Name
|
Field Type
|
Vendors
|
Vendors
|
Vendor Name
|
Vendor_Name
|
Name
|
Vendor Email
|
Vendor_Email
|
Email
|
Customers
|
Customers
|
Customer Name
|
Customer_Name
|
Name
|
Email
|
Customer_Email
|
Email
|
Address
|
Address
|
Address
|
Products
|
Products
|
Product Name
|
Product_Name
|
Single Line
|
Stock
|
Stock
|
Number
|
Vendor
|
Vendor
|
Lookup (Vendor)
|
Customers With Restock Request
|
Customers_With_Restock_Request
|
Customer Name
|
Customer
|
Lookup (Customer)
|
Product Name
|
Product
|
Lookup (Products)
|
Mailed
|
Mailed
|
Decision Box
|
Number Of Items Needed
|
Number_Of_Items_needed
|
Number
|
Orders
|
Orders
|
Customer Name
|
Customer
|
Lookup
(Customers)
|
Order Details
-
Product Name
-
Quantity
-
Number of Items Needed
|
Order_Details
-
Product_Name
-
Quantity
-
Number_of_Items_Needed
|
Subform
-
Lookup (Products)
-
Number
-
Number
|
2.
Create a workflow
that will be triggered every time the customer updates the
Quantity field in the subform of the Orders form.
3. Click
Add New Action and
save the following deluge snippet in the deluge editor to calculate the number of items needed if the customer requested for more items:
- //set the value of the Number of Items Needed for the current subform row to be 0
- row.Number_Of_Items_Needed=0;
- //fetch the corresponding Products record based on the Product Name field in the subform
- product = Products[ID == row.Products];
- //Compare the value entered by the customer in the Quantity field with the Stock value of the chosen Product and calculate the number of items needed if the requested Quantity is less than the Stock
- if(row.Quantity > product.Stock)
- {
- //Alert if the Quantity value is reset to the available Stock value
- alert "Sorry, we have only " + product.Stock + " items. We will add this to your cart now. We will notify when we get the stock from our vendor.";
- row.Number_Of_Items_Needed=row.Quantity - product.Stock;
- row.Quantity=product.Stock;
- }
- //Re-Calculate new total values
- row.Sub_Total=row.Quantity * row.Rate;
4.
Create another workflow
that will be triggered every time a record is added to
the
Orders
form (successful form submission).
5. Click
Add New Action
and save the following deluge snippet in the deluge editor to notify the stakeholders, Vendors and Customers based on the number of items:
For the benefit of understanding, the Deluge code is split into snippets. We explain the snippets and then arrange them appropriately for inserting them to the Deluge Editor.
Please check the attachments for a file with the following snippets consolidated into one working script.
Snippet a
The notification to the customers is based on the field value of the
Number of Items Needed
. Let us analyse it based on the value of this field. If the value is more than 0, a record is inserted into the
Customers With Restock Request
with the
Number of Items Needed
value.
- //Tracking the restock requesting customer details
- //Check if a request with same customer and product is already there
- customers_with_restock_request = Customers_With_Restock_Request[Products == product.ID && Customer == input.Customer && Mailed == false].count();
- if(customers_with_restock_request > 0)
- {
- //The customer who had previously raised a request
- customers_with_restock_request = Customers_With_Restock_Request[Products == product.ID && Customer == input.Customer && Mailed == false];
- customers_with_restock_request.Number_Of_Items_needed=customers_with_restock_request.Number_Of_Items_needed - row.Number_Of_Items_Needed;
- }
- else
- {
- //A New customer with restock request
- insert into Customers_With_Restock_Request
- [
- Added_User=zoho.loginuser
- Customer=input.Customer
- Mailed=false
- Number_Of_Items_needed=row.Number_Of_Items_Needed
- Products=row.Products
- ]
- }
Snippet b
The
Vendor
is
e
mailed highlighting this and
Stock
field of the corresponding
Products
record is set as 0.
- //Mail the Vendor
- sendmail
- [
- from :zoho.adminuserid
- to :vendor.Email
- subject :"Out Of Stock"
- message :"<div><div>Hi, " + vendor.Vendor_Name + ",<br></div><div><br></div><div>Recently our product, " + product.Product_Name + " has gone out of stock. Kindly restock.<br></div><div><br></div><div>Thank you.<br></div><div><br></div></div><div><br></div>"
- ]
- //Set the Stock field of the corresponding Products record to be 0
- product.Stock=0;
Snippet c
If the customer's request is well under the Stock value of the Products record, we shall add the row and update the Stock value. Proactively, we mail the Vendor if the Stock becomes less
than a threshold value, say, 50 items.
- //Calculate new stock
- product.Stock=product.Stock - row.Quantity;
- //Mail Vendors when numbers go less than 50
- if(product.Stock <= 50)
- {
- sendmail
- [
- from :zoho.adminuserid
- to :vendor.Email
- subject :product.Product_Name + " is running out!"
- message :"<div><div>Hi, " + vendor.Vendor_Name + ",<br></div><div><br></div><div>Recently our product, " + product.Product_Name + " is being most sought after, that we are running out of it. Only " + product.Stock + " remain! Kindly restock.<br></div><div><br></div><div>Thank you.<br></div><div><br></div></div><div><br></div>"
- ]
- }
Snippet d
Now, we shall combine Snippet a, Snippet b, and Snippet c; the snippets need to be
looped through the total number of rows
inserted into the subform. The Products and the Vendor records are found
fetched
based on the value chosen in the subform row.
- //Assign the subform of the current record to a variable
- subform = input.Inline_Subform;
- for each row in subform
- {
- //Fetch the relevant product
- product = Products[ID == row.Products];
- //Get the Vendor
- vendor = product.Vendor;
- if(row.Number_Of_Items_Needed > 0)
- {
- //Insert Snippet a & b
- }
- else
- {
- //Insert Snippet c
- }
- }
6. Create a workflow on the
Product
form, on the successful form submission when a product record is edited.
7. Click
Add New Action
and save the following deluge snippet in the deluge editor to
e
mail customers accordingly for the
Stock
field updated successfully:
- //Assigning subject and message for sending email to the customers
- subject = "";
- message = "";
- if(input.Stock > 0)
- {
- //This condition ensures that mailing is only for restocked
- //Mail only those customers who asked for that product
- for each customer in Customers_With_Restock_Request[Products == input.ID && Mailed == false]
- {
- //Check the customer restock request items is equal to the updated Stock and mail accordingly
- if(customer.Number_Of_Items_needed <= input.Stock)
- {
- subject = input.Product_Name + " is now re-Stocked!";
- message = "<div>Hello<br></div><div><br></div><div>" + input.Product_Name + " is now restocked with " + input.Stock + " items. Order now!</div>";
- //Mark them as mailed to stop from spamming
- customer.Mailed=true;
- }
- else
- {
- //Mail when the number of restock items is less than what the customer had requested
- subject = input.Product_Name + " is re-Stocked, but...";
- message = "<div>Hello<br></div><div><br></div><div>" + input.Product_Name + " is now restocked with " + input.Stock + " items. We know you had asked for " + customer.Number_Of_Items_needed + " items. We were able to procure a lesser number. Sorry! We will notify when we get the remaining items!</div>";
- }
- sendmail
- [
- from :zoho.adminuserid
- to :customer.Customer.Email
- subject :subject
- message :message
- ]
- //Since the customer should be mailed again, Mailed is not updated
- }
- }
See how it works