Tip 32: How to set form field values using custom functions

Tip 32: How to set form field values using custom functions

Hi folks,

We're back today with our next tip—how to set form field values using custom functions. Before we get started, though, let's take a look at the basics.

What do you mean by setting values for a field in a form?

To assign a value to a form field based on a particular calculation or condition.

What are custom functions?

Custom functions are created by the user or developer to perform a particular action or process that's unique. It's often used when there is a need to repeat the same custom process many times without having to rewrite the code. For example, you can create a custom function to calculate the total price every time a user enters a discount code. Also note that a custom function can be reused anywhere within an application or across applications, saving you time and effort.

Common examples of using custom functions to:
  • Effectively manipulate data
  • Communicate with third-party applications
  • Integrate custom actions in Zoho Creator reports
Setting up a custom function includes the following steps: 
  1. Create a custom function.
  2. Test the custom function for accuracy.
  3. Associate the custom function with a workflow rule.
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. 
  1. Specify the function name as calulatemarks.
  2. Add a new namespace and name it Calculations.
  3. Select Map as the return type. 
  4. Specify arguments sub1, sub2, sub3, sub4, and sub5 as float types.
  5. 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.

  1. map calulatemarks(float sub1, float sub2, float sub3, float sub4, float sub5)
  2. {
  3.  tot = sub1 + sub2 + sub3 + sub4 + sub5;
  4.  perc = ((tot / 500) * 100);
  5.  if(sub1 > 35.00 && sub2 > 35.00 && sub3 > 35.00 && sub4 > 35.00 && sub5 > 35.00)
  6.  {
  7.   res = "Pass";
  8.  }
  9.  else
  10.  {
  11.   res = "Fail";
  12.  }
  13.  mp = Map();
  14.  mp.put("Total",tot);
  15.  mp.put("percerntage",perc);
  16.  mp.put("result",res);
  17.  return mp;
  18. }
Step 3: Trigger the custom function on the On User Input condition

Once the function is created, we need to call this function in the On User Input condition of all the fields used in the calculation to automate it. Also, we have to ensure that we choose the form fields correctly, with respect to the function arguments.



Step 4: Assign the values to the form fields

The custom function will return three values (Total Marks, Percentage, and Result). We have to assign those values into the form fields. Since we've used the Map return type, we can use the key declared in the function to get the calculated value and assign that variable to form fields.

  1. output = thisapp.Calculations.calulatemarks(input.Subject_1,input.Subject_2,input.Subject_3,input.Subject_4,input.Subject_5);
  2. input.Total = output.get("Total");
  3. input.Percentage=output.get("percentage");
  4. input.Result = output.get("result");

Here, we've illustrated it with simple calculation. However, based on your business use case, the calculations can be more complex, as well. The above discussed way of using form fields inside of custom functions will help you perform operations inside of custom functions as source objects, and use them wherever you need. 

We hope you liked this tip and decide to try it out. If you hit any roadblocks, feel free to reach out to us and we'll be happy to assist you.





                            Zoho Desk Resources

                            • Desk Community Learning Series


                            • Digest


                            • Functions


                            • Meetups


                            • Kbase


                            • Resources


                            • Glossary


                            • Desk Marketplace


                            • MVP Corner


                            • Word of the Day



                                Zoho Marketing Automation


                                        Manage your brands on social media



                                              Zoho TeamInbox Resources

                                                Zoho DataPrep Resources



                                                  Zoho CRM Plus Resources

                                                    Zoho Books Resources


                                                      Zoho Subscriptions Resources

                                                        Zoho Projects Resources


                                                          Zoho Sprints Resources


                                                            Qntrl Resources


                                                              Zoho Creator Resources


                                                                Zoho WorkDrive Resources



                                                                  Zoho Campaigns Resources

                                                                    Zoho CRM Resources

                                                                    • CRM Community Learning Series

                                                                      CRM Community Learning Series


                                                                    • Tips

                                                                      Tips

                                                                    • Functions

                                                                      Functions

                                                                    • Meetups

                                                                      Meetups

                                                                    • Kbase

                                                                      Kbase

                                                                    • Resources

                                                                      Resources

                                                                    • Digest

                                                                      Digest

                                                                    • CRM Marketplace

                                                                      CRM Marketplace

                                                                    • MVP Corner

                                                                      MVP Corner

                                                                    





                                                                    




                                                                        Design. Discuss. Deliver.

                                                                        Create visually engaging stories with Zoho Show.

                                                                        Get Started Now