Add Business Logic
Level: Beginner Duration: 30 minutes
Recap: At this point, you know how to create an application in Zoho Creator, capture data through forms, create relationship between forms, create reports, customize the user interface and share the app with your team or customers. In this tutorial, you will learn to add simple logic to your application using Deluge Scripting.
Table of Contents
Every application created in Zoho Creator has an equivalent Deluge Script file generated at the backend with extension .ds. For example, when you create an Order management application, a file named OrderManagement.ds is generated at the backend that contains the script definition of all the Forms and Reports in the application.
Screenshot displaying order form and its equivalent script
The script definition of the Order Form as defined in OrderManagement.ds is explained below.
Form: Form is the structure that contains the data. It is referred in two ways:
- Form Name is the display name by which a form will be referred throughout in the GUI mode and also while a user is accessing the application in Live Mode. The display name should be given within double quotes.
- Form Link Name is the actual name of the form. The link name of a form is unique within an application and will be referred in scripting. The link name should be alphanumeric and can contain an underscore. When you create a form with name, for example, "Add Employee", the form link name will be auto-generated as "Add_Employee". If form display name and link name is same, for example, "Orders", it will not be explicitly defined in the script.
Form Fields: The fields define the individual properties of the specific kind of information. A form field is referred in two ways.
- Field display name is the display name by which a field will be referred throughout in the GUI mode and also while a user is accessing the application in Live Mode. The display name should be given within double quotes. For example, "Order Date"
- Field link name is the actual name of the field and is unique within a form. Only the link name is referred in scripting. The link name should be alphanumeric and can contain an underscore. For example, Order_Date
- Type refers to the specific kind of information that is stored in the field. For example, Text field type for Names, email field type for email address, date field type for date-time values like Order_Date.
- width refers to the width of the form field in pixels.
displayname = "Order Date"
type = date
width = 130px
alloweddays = 0,1,2,3,4,5,6
In this session, we will make a few enhancements to the "Orders" form by adding logic to it. The following is a list of enhancements that we are going to add.
When a user clicks on the Order Form to submit an order, we want the Order form to display the current date in the Date field, the email id of the logged in user in the email field and a default delivery option selected. To execute these actions when a form is loaded, we have to add the script in the Form Actions - on add-on load block of the script builder.
Steps to add the script :
- Select the "Orders" form
- Click on Form Settings > Workflow - On Add > Actions on Load. This will display the script builder.
- The script to be added is shown below, along with comments explaining the script://Disable Order Date field since it is not required to be edited by the user
//Use the disable task
//Set current date in Order Date field
//Use set variable task
input.Order_Date = zoho.currentdate;
//Set Email field with the logged in user email ID
//Use set variable task
input.Email = zoho.loginuserid;
//Set default delivery option
//Use select function
Delivery.select("USPS Priority (1 - 2 days)");
Click on Save. When the form is accessed in live mode, it will display the values as shown in the screen-shot below.
Screenshot displaying the form in live mode
- The above screenshot displays the disabled "Order Date" field, set to the current date. The field is disabled using the "Disable" function, which is known as a Client Actions. Client Actions help interaction in a form.
- The current date is set using the system variable "zoho.currentdate". System Variables are predefined variables which contain useful information like current date, current time, username and email of logged-in user etc.
- Using the system variable "zoho.loginuserid", the email field displays the Email Id of the logged in user. Let's assume "John" is the user in this case.
- The delivery type is also pre-set using the "Select" Client Action. "USPS Priority (1 - 2 days)" is known as a literal. Literals are fixed values and can be of different data types.
Now, we have to set the delivery option based on the number of products selected by the user. For example, if the number of products selected is greater than 5, the delivery option should be set as "3 - 5 days". To do this, we will add a script to the "Products" field. This script is called "On User Input" script and will be executed when a user has to input the required products in the Products field. The On User input is a client-side action which will be called whenever the value of a field is modified either by the user or through the script.
Steps to add the On User Input script
- Select the "Orders" form
- Click on Workflow - Field Actions - Products - On User Input
- The script to be added is shown below, along with comments explaining the script://Set condition of greater than 3 and less than 5 products
//Use If task
if (input.Products.size() > 3 && input.Products.size() <=5)
//Set the default delivery
//Use Set Variable task
Delivery.select("USPS 1st class (2 - 3 days)");
//Set condition of greater than 5 products in
//Use else if task
else if (input.Products.size() > 5)
//Set default delivery
//Use Set variable task
Delivery.select("USPS standard (3 - 4 days)");
- Click on "Save script" to add the script definition to the form.
- According to the code, if the number of products is greater than 3 and less than or equal to 5, then "USPS First Class (2 - 3) days" delivery type is selected. If the number of products exceeds 5 then "USPS Standard (2 - 8 days)" delivery type is selected. In this code we have used the following:
- Relational Expressions - Relational Expressions compare two given values and return true or false. In this example, we have used "Greater than" and "Less than or equal to" Relational Expressions. The following is a list of Relational Expressions:
- Equals ( == )
- Not Equals ( != )
- Greater than ( > )
- Greater than or equal to ( >= )
- Less than ( < )
- Less than or equal to ( <= )
- Conditional Expressions - Conditional Expressions combine two or more boolean values to return another boolean value. In this example, we have used && (AND)Conditional Expression to combine two boolean values. The following is a list of Conditional Expressions:
- And ( && )
- Or ( || )
- Not ( ! )
- Built-In Functions - Built-In Functions are pre-defined functions that can be called to perform useful actions. For example, here we have used the "size()" built-in function which belongs to MAP functions. The "size()" function is used to calculate the size of a map variable, (in this case, the field Products is a map variable).
- Zoho Creator supports predefined functions, also called as Built-in Functions under the following categories.
- String Functions
- Math Functions
- Date-Time Functions
- Logical Functions
- List Functions
- Map Functions
- XML Functions
- URL Functions
- Form Data Functions
- Common Functions
- Decision - Decision helps to execute different logic for different conditions. In this example we have used "if" and "else if" functions to create different conditions for different logic to be executed in each case.
A valid order requires at least two number of products to be selected. If it is less than 2, the order will not be accepted and the form submission will be canceled. To validate the form data (in this case, the number of products selected), we will add a script to the "validate" section of the form. This script is executed when a user submits the form and before the data is stored in the database.
Steps to add Validate script
- Select the "Orders" form
- Click on Form Settings - On Add - Validate on Form Success. This will display the script builder.
- The script to be added is shown below, along with comments explaining the script:
//if condition to check the number of products selected
//Use if task
if(input.Products.size() < 2)
//Message to be displayed if validation fails
//Use alert task
alert ("Select atleast 2 products");
//cancels the submission
- Click on "Save script" to add the script to the form definition.
The validate script displays an alert message to the user if less than 2 products are selected and cancels the form submission.
When a user successfully submits an order, an email must be sent to the user, confirming that the order has been received with the order id. To do this, we will add a script to the On Success block of the form. The on success script is executed after a record is successfully submitted to the database.
Steps to configure on success script
- Select the "Orders" form
- Click on Form Settings > Workflow > On Add - Actions on Success. This will display the script builder.
- Drag and drop the Send Mail task.
- Fill in the box as shown in the screenshot below.
5. Click on "Done" to save the changes. Now you will land on the script builder page which will contain the equivalent script of the "Send Mail" task. The script is shown below.
subject:"Thank you for your order"
message:"Hi" + zoho.loginuser + "Thank you for placing an order with us. Your order ID is" + input.ID
6. Click on Save Script to add the script to the form definition.
- The send mail task sends an email using the data specified in the Send Mail task fields. This task is executed upon submission of the form.
- The mail is sent to the logged in user, using the system variable zoho.loginuserid, specified in the "To" field.
- Similarly, the mail will be sent from the admin user, using the system variable zoho.adminuserid, specified in the "From" field.
- The "Subject" and "Message" fields allow you to add the subject and content of your mail respectively.