Create and manage Java functions | Zoho Creator Help

Create and manage Java functions

A function is a set of statements that is invoked in an application to perform a certain action or yield a desired result based on the logic in the code. Java is a widely used programming language. Introducing Java will equip you to implement the concepts that are native to Java programming in your Creator application.

Create a new function using java

To create a new java function:

  1. Click on Workflows to navigate to your Workflow Dashboard.

  2. Click Functions tab.

  3. Click New Function button. The create page will appear.

  4. Enter Function Name. This serves as the identifier using which the function can be invoked. Specify a meaningful name for the function without any empty spaces.

  5. Select Java as Language for scripting the function.

  6. Specify the Arguments.
  7. Click Create Function button. The builder will appear.
  8. Now, Add required java code to the function and click Done.

  9. The function will get listed under Functions in the workflow dashboard.

 

Note :

  • Namespace : Namespace for java function will be  Default Namespace .
  • Return Type : Return value of a java function will only be of  Collection data type .
  • Argument : Java functions accept only  string  values.
  • Java functions cannot be renamed after creation.

Arguments and parameters in java functions

Similar to deluge function, java also requires you to specify arguments based on which your function runs. An argument is the instance that is passed to the method on running a function. At the event of creating a java function, you can specify an argument. If you have the necessity to add more arguments to the same function at a later stage, you can access the  Function Properties  in the builder (click the  Settings  icon).

To run a program, you need to define parameters in java functions. A parameter is the variable assigned for the value of an argument. It serves as the container in which the argument value passed from deluge is received in a java function.

Sample function

This function is created to calculate each employee's pay in java function and use the values obtained to update records using deluge.

fetchRecords will create a collection data that contains each record in Employee_Details form in {employe_id : working hours } format. After filling the data, we will pass it in string format to "calculateSalary" java function.

The java function receives Employee_Details map, and calculates salary based on working hours, for each Employee ID, and passes those details as another map payDetails in {"employe_id : salary } format.

We have assigned java function return value to "payCalc" collection data in deluge function. By iterating payCalc value, we can update "Weekly_Pay" field with salary detail.

void fetchRecords()
{
recDetails = Map();
for each i in Employee_Details[ID != 0]
{
recDetails.put(i.Emp_ID,i.Weekly_Hours);
}
payCalc = Collection();
payCalc = thisapp.calculateSalary(recDetails.toString());
updateDetails = payCalc.get("output").toMap();
keys = List();
keys = updateDetails.keys();
for each i in keys
{
info i;
x = Employee_Details[Emp_ID == i];
x.Weekly_Pay=updateDetails.get(i);
}
}

 

import com.zoho.cloud.function.Context;
import com.zoho.cloud.function.basic.*;
import org.json.*;
import org.json.JSONObject;
import com.zohocreator.test.EmployeeSalary;

public class calculateSalary implements ZCFunction
{
   public void runner(Context context, BasicIO basicIO) throws Exception
   {
      try
      {
         String dataMap = (String)(basicIO.getParameter("recDetails")); // Retrieve arguments

         JSONObject dataObject = new JSONObject(dataMap); // Converting string arguments to JSONObject

         EmployeeSalary salary = new EmployeeSalary(); // creating object for "EmployeeSalary" class
         JSONObject payDetails = salary.calculate(dataObject); //calling calculate method in "EmployeeSalary" class

         basicIO.write(payDetails.toString()); // Return data back to deluge script
      }
      catch(Exception e)
      {
        context.log(e.getMessage());
      }

      }
}

Note :

  • By default, the function and the class name are the same, and should be not be changed.
  • Execution begins with the runner method, and it should not be removed. This means that you need not add a main() after the runner method to write your script logic.
  • If a user wants to write a new class or function, it needs to be written after the runner method and called from the runner method.
Call a java function

To call a java function in a deluge script, you need to use the statement indicated in the image below. In this example, “payCalc = thisapp.calculateSalary(recDetails.toString());” refers to the java function, which contains the value as collection data value.

Collection data type

Java function returns a string value only. If your process is satisfied with the string data returned, you can go ahead with it. If you require any other data type like integer, you can assign collection data type to your return value. It can be used for further processing like updating records with the values derived using the java function. For instance, the pay calculated in java function will return string values. When the assigned collection data value is used in deluge these string values yield integer values that are used to update records in the application. 

This collection object allows you to make a loop to execute this function for all the records that are there in the form. The collection object will include the employee ID (key) and the hours (value). If there are 20 records in the form, the 20 together become one collection object enabling the java function to be executed for the 20 records in one function call.

Get Value

Using the basicIO.getParameter statement you will be able to obtain the values that are contained as arguments. The values that you have specified as arguments need to be passed in your java function for you to process the values and get the desired results. In this case, Hours Worked and Salary are the arguments. The values are passed in the java function to calculate the wages using the getParameter statement. 

Output

A java function output value will by default be a "Collection Value". This output will have two keys:

  • Output key has value which is the output data
  • Log key has log data which user print using statement

The output key will include the following:

  • context.log: The statement used to print logs. We can get the values added inside "log" key of a java function output map. The information in context.log is used for debugging purpose.
  • basicIO.write: The statement used to send to deluge the return value after calculation to the place in Creator from where this function is called. We can get values added inside "output" key of java function output map.​
Config.json and Library
  • config.json is a configuration file.
  • lib is the library to which you can upload your Jar files and packages. Right click on the folder to upload files.​

  • In the above image Point 1 - The JAR file that has been uploaded. (Click  here  to download the JAR file)
  • Point 2 - Import of the class in the JAR file into the java code.
  • Point 3 - An object is created and calculate method is called here.
  • The JAR file will do the calculation that if the working hours was less than 45 then the pay would be 300 and for extra hours it would be 100 per hour.

Points to remember

  • Jar files and packages can only be uploaded to the library.
  • The file upload limit of all the files put together is 10 MB.
  • The size of your request and response payload can only be up to 1 MB each.
  • The file size in the editor can only be up to 1 MB.
  • You can pass a maximum of 20 arguments to java functions
  • You can call a function 5 times in one deluge script.
  • You need to receive a response within 40 seconds from the start of a function call, beyond which the execution will fail.
  • Following are the inbuilt jars:
    1. commons-io-2.4.jar
    2. json.jar (org.json)
  • Currently this feature is not supported in DS files. The cloud functions will not be captured during import/export/backup/duplicate actions. Instead it will be substituted with an empty deluge function of same name to avoid errors.
  • system.out.println  statement will not work in creator cloud functions


    Access your files securely from anywhere

      Zoho CRM Training Programs

      Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

      Zoho CRM Training
        Redefine the way you work
        with Zoho Workplace

          Zoho DataPrep Personalized Demo

          If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

          Zoho CRM Training

            Create, share, and deliver

            beautiful slides from anywhere.

            Get Started Now


              Zoho Sign now offers specialized one-on-one training for both administrators and developers.

              BOOK A SESSION









                                            You are currently viewing the help pages of Qntrl’s earlier version. Click here to view our latest version—Qntrl 3.0's help articles.




                                                Manage your brands on social media

                                                  Zoho Desk Resources

                                                  • Desk Community Learning Series


                                                  • Digest


                                                  • Functions


                                                  • Meetups


                                                  • Kbase


                                                  • Resources


                                                  • Glossary


                                                  • Desk Marketplace


                                                  • MVP Corner


                                                  • Word of the Day


                                                    Zoho Marketing Automation

                                                      Zoho Sheet Resources

                                                       

                                                          Zoho Forms Resources


                                                            Secure your business
                                                            communication with Zoho Mail


                                                            Mail on the move with
                                                            Zoho Mail mobile application

                                                              Stay on top of your schedule
                                                              at all times


                                                              Carry your calendar with you
                                                              Anytime, anywhere




                                                                    Zoho Sign Resources

                                                                      Sign, Paperless!

                                                                      Sign and send business documents on the go!

                                                                      Get Started Now




                                                                              Zoho TeamInbox Resources



                                                                                      Zoho DataPrep Resources



                                                                                        Zoho DataPrep Demo

                                                                                        Get a personalized demo or POC

                                                                                        REGISTER NOW


                                                                                          Design. Discuss. Deliver.

                                                                                          Create visually engaging stories with Zoho Show.

                                                                                          Get Started Now









                                                                                                              • Related Articles

                                                                                                              • Create and manage node.js functions

                                                                                                                A function is a set of statements that is invoked in an application to perform a certain action or yield a desired result based on the logic in the code. Node.js is a cross-platform, open-source JavaScript runtime environment. Introducing Node.js ...
                                                                                                              • Understand functions

                                                                                                                A function is a set of deluge or java statements grouped together, which can be invoked within an application whenever required. Use functions to structure deluge scripts in a modular way. Related functions can be grouped under a common category, ...
                                                                                                              • Create and Manage Extensions

                                                                                                                Build and Distribute Extension Extensions are the software components or add-ons that let you extend the functionalities of Zoho Creator. Learn more about extensions. Note: Currently, you can only build widgets as extensions in your Creator. Step 1: ...
                                                                                                              • Create and Manage Batch Workflows

                                                                                                                1. What Does This Page Cover? Learn how you can efficiently split your larger tasks into smaller batches and process them at off-peak hours using batch workflows. Before you proceed to create a batch workflow, check this page to understand about ...
                                                                                                              • Create and Manage Custom APIs

                                                                                                                This help page is for users in Creator 6. Find your Creator version. 1. What Does This Page Cover? Learn how to create a custom API and authenticate it to integrate with in Zoho Creator. Before moving ahead, click here to learn more about custom ...
                                                                                                                Wherever you are is as good as
                                                                                                                your workplace

                                                                                                                  Resources

                                                                                                                  Videos

                                                                                                                  Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.



                                                                                                                  eBooks

                                                                                                                  Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.



                                                                                                                  Webinars

                                                                                                                  Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.



                                                                                                                  CRM Tips

                                                                                                                  Make the most of Zoho CRM with these useful tips.



                                                                                                                    Zoho Show Resources