In this tip, you'll learn the basics of accessing the form fields inside of a custom function and how they can be used in Zoho Creator. Let's take a simple example of calculating the total score for marks obtained in 5 different subjects by each student in a class. This includes calculating their percentage and evaluating if they failed or passed the examination.
So, instead of repeating the same calculation for each student, we can create a custom function to perform the calculations based on form fields and call that function in form workflows. Let's look at it step by step.
Note: The objective here is to calculate the total marks [M1 + M2 + M3 + M4 + M5] obtained in 5 different subjects and the corresponding percentage.
Step 1: Create the form, with all the required fields
First, let's create a Student Marks form to capture the scores for a student in 5 different subjects, plus other basic details. Here, we've used the following fields:
- Student Name (Name field)
- Roll No (Single Line field)
- Class (Single Line field)
- Subject 1(Decimal field)
- Subject 2 ( Decimal field)
- Subject 3 (Decimal field)
- Subject 4 (Decimal field)
- Subject 5 (Decimal field)
- Total Marks (Decimal field)
- Percentage (Percentage field)
- Result (Dropdown field)
Note: Please note that we have to provide initial values of 0.0 for all the fields involved in calculations, to avoid any runtime errors.
Step 2: Create the custom function to calculate the Total Marks, Percentage, and Result.
Now we have to create a custom function that will help us make all the calculations.
In this case, we'll use the Map as the return type of the function, as we're collecting more than one value in the custom function. We can also use the List type for this function. So, before we define the calculation that needs to be done behind the scenes, the first thing that we need to do is define them all as arguments of the function with their respective data types.
- Specify the function name as calulatemarks.
- Add a new namespace and name it Calculations.
- Select Map as the return type.
- Specify arguments sub1, sub2, sub3, sub4, and sub5 as float types.
- Click Create Function.
Once a function is created, you can write the script as per your requirements using function arguments. These arguments hold the form field values when called in the form workflows.