Functions

Functions

This guide helps you with the following:
  1. Create Functions 
    1. Normal Functions
    2. REST API Functions
  2. Invoke Functions 
  3. Edit Functions 
  4. Delete Functions
  5. Rest API Functions
    1. Authentication
    2. Version 2.0
    3. Version 1.0
    4. Example
    5. Comparison of Version 2.0 and 1.0
    6. Invoke REST API functions from widgets

Functions are the user-defined type of routines that performs a specific set of operations whenever called. They are defined in Zoho Developer as stand-alone functions written in Deluge Scripts and are module independent. So, these functions can be called inside any of the workflows to achieve the specified functionality.

Return Type and Arguments

  1. Single or multiple arguments of supported data types can be passed as parameters to the functions.
  2. Functions can return values with one of the supported data types.
  3. The supported data types are Integer,Float,String,Boolean,Date,Map and List.

Types of Functions

Functions in Zoho Developer are categorised as Normal functions and REST API functions.

Create Functions

Normal Functions

These are the functions invoked in other workflow custom functions, custom buttons, Related lists and schedules.

To create a function,
  1. Log in to the Zoho Developer Console and click Extensions for Zoho CRM.
  2. Select the extension you want to create a function for, and click Edit.
  3. Click Automate in the left pane, then click Functions.
  4. Click Create Function in Functions page.
  5. In the Create Function page, fill in the following details,
    1. Name of the function
    2. Invoke as REST API - "No" for normal functions and "Yes" for REST API functions.
    3. Select the return type of the function
    4. Click Add Arguments if any,and specify the arg name and type
  6. Click Save.
A sample code snippet in Deluge Script for a normal function is given below,
  1. string pluginname.Copy_Address(string module, int id)
  2. {
  3. m = { "module" : input.module, "id" : input.id };
  4. resp = zoho.crm.invokeConnector(("crm.get"), m);
  5. response = (resp.get("response")).toMap();
  6. data = response.get("data").toJSONList();
  7. respstr = "";
  8. for each rec in data
  9. {
  10. dataMap = rec.toMap();
  11. idd = dataMap.get("id");
  12. street = dataMap.get("Street");
  13. city = dataMap.get("City");
  14. state = dataMap.get("State");
  15. country = dataMap.get("Country");
  16. zip = dataMap.get("Zip_Code");
  17. updateDataMap = map();
  18. updateDataMap.put("installscript.Billing_Street", street);
  19. updateDataMap.put("installscript.Billing_City", city);
  20. updateDataMap.put("installscript.Billing_State", state);
  21. updateDataMap.put("installscript.Billing_ZipCode", zip);
  22. updateDataMap.put("installscript.Billing_Country", country);
  23. updateDataMap.put("id", idd);
  24. l = List();
  25. l.add(updateDataMap);
  26. updMap = { "module" : input.module, "data" : l };
  27. updateResp = zoho.crm.invokeConnector("crm.update", updMap);
  28. respstr = updateResp+"";
  29. }
  30. return respstr;
  31. }

REST API Functions

The REST API function provides you the flexibility of triggering it from anywhere - within a function in an extension or from a third-party application. Third-party applications can invoke these API functions as webhook URL. More details on REST API Functions can be found here.

Invoke Functions

Stand-alone functions are invoked inside custom functions,custom buttons,related lists and schedules as given below,
  1. resp = <EXTENSIONNAME>.Copy_Address("Leads", <LEADID>);
The "Copy_Address" function generates output API response as,
  1. {"response":"{\"data\":[{\"code\":\"SUCCESS\",\"details\":
  2. {\"Modified_Time\":\"2017-08-29T14:53:08+05:30\",\"Modified_By\":
  3. {\"name\":\"Saranya \",\"id\":\"2641973000000124009\"},
  4. \"Created_Time\":\"2017-08-29T14:28:16+05:30\",
  5. \"id\":\"2641973000000128184\",\"Created_By\":{\"name\":\"Saranya \",
  6. \"id\":\"2641973000000124009\"}},\"message\":\"record
  7. updated\",\"status\":\"success\"}]}","status_code":200}
A Rest API function can be invoked similar to a normal function wherever it is necessary.

Edit Functions

To edit a function
  1. Log in to your Zoho Developer Console and select Extensions for Zoho CRM.
  2. Select the extension your function is associated with, and click Edit.
  3. Click Automate option in the left pane, then click Functions.
  4. Select the required function from the list and click the Edit icon [].
  5. Edit and update the changes as per your requirements.

Delete Functions

To delete a function
  1. Log in to your Zoho Developer Console account and select Extensions for Zoho CRM.
  2. Select the extension associated with the function you wish to delete, and click Delete.
  3. Click Automate option in the left pane and click Functions.
  4. Choose the function that you want to delete, and click the Delete icon.
  1. Calling a standalone function inside another standalone function is not supported.
  2. Writing a function for Install Actions are also not supported.

REST API Functions

The REST API function provides you the flexibility of triggering it from anywhere - within a function in an extension or from a third-party application. Third-party applications can invoke these API functions as webhook URL. There are two versions of the REST API function (explained in detail in later sections).

Authentication

The REST API function uses the API Key authentication method. The value of the ZAPI key to be used can be obtained using one of the following:
  1. Custom Functions
  2. Installation scripts

Using Custom Functions

  1. Publish the extension in which the REST API Function is included and install the extension.
  2. Log in to your application.
  3. Go to Setup > Automation > Actions and select the Functions tab.
  4. Click the Configure Function button.
  5. Click the Write your own link.
  6. In the Deluge Script Editor, paste the following code snippet and click Save & Execute Script. 
  1. m = {"nameSpace" : "<portal_name.extension_namespace>"};
  2. apikeyresp = zoho.crm.invokeConnector("crm.zapikey",m);
  3. zapikey = apikeyresp.get("response");
  4. info zapikey;
      7. The ZAPI key will be displayed in the Log messages.

Using Installation Scripts

  1. Log in to your Zoho Developer console and click Extensions for Zoho CRM.
  2. Select the extension in which the REST API Function is included, then click Edit.
  3. Select Custom Properties in the left pane.
  4. Click the Create button.
  5. Enter a variable name in  Field Name and API Name fields and click Save.
    This variable will be used in the installation script to store the URL of the REST API function (details are explained in the steps below).


      6. Select Install Actions in the left pane.
      7. Click Extension - On Installation.
      8. In the Deluge Script Editor, paste the below code snippet and click Save.  
  1. m = { "nameSpace" : "<portal_name.extension_namespace>" };
  2. apikeyresp = zoho.crm.invokeConnector("crm.zapikey", m);
  3. zapikey = apikeyresp.get("response");
  4. url = "https://platform.zoho.com/crm/v2/settings/custom_functions/<extension_namespace.
  5.         function_name>/execute?zapikey=" + zapikey;
  6. m = { "apiname" : "<extension_namespace.variable_name>", "value" : url };
  7. r = zoho.crm.invokeConnector("crm.set", m);
  8. info r;
      9. Publish the extension in which the REST API Function is included and install the extension.
      10. Log in to your application.
      11. Go to  Setup > Marketplace > All > Installed and click the extension name.
      The value of the variable "apifunctionurl" will be displayed under the Extension Settings section of the Extension Details page. This variable will have the ZAPI key value.

Version 2.0
Details of the Version 2.0 of the REST API function are given below:

Request Object

The entire request object of the function is stored in crmAPIRequest. The need to create multiple arguments within the function is eliminated as the crmAPIRequest object dynamically stores the information from the request and makes it available inside the function. The request object will be automatically mapped to the crmAPIRequest argument.

Input Types





The input for the function can be acquired from the following types:


Body

Sending body using POSTMAN as a stream (raw): 


Sending body using POSTMAN as a binary file:


To get the entire body section of the request in a function:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. request_body = crmAPIRequestMap.get("body");
  3. company = request_body.get("Company");
  4. name = request_body.get("last_name");
  5. m = Map();
  6. m.put("Last_Name",name);
  7. m.put("Company",company);
  8. r = zoho.crm.createRecord("Leads",m);
  9. return "Lead created successfully ::: ";
In POSTMAN, the body content can be passed either in the raw or binary.

Parameters

Sending parameters using POSTMAN, within the request URL:


Sending parameters using POSTMAN as KEYS in form-data:


To get the additional parameters used in the request:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. parameters = crmAPIRequestMap.get("params");
  3. company = parameters.get("Company");
  4. name = parameters.get("last_Name");
  5. m = Map();
  6. m.put("Last_Name",name);
  7. m.put("Company",company);
  8. r = zoho.crm.createRecord("Leads",m);
  9. return "Lead created successfully ::: ";
Sending parameters using POSTMAN, as JSON objects in form-data:



To get the additional parameters used in the request:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. parameters = crmAPIRequestMap.get("params");
  3. args = parameters.get("args");
  4. company = args.get("Company");
  5. name = args.get("last_name");
  6. m = Map();
  7. m.put("Last_Name",name);
  8. m.put("Company",company);
  9. r = zoho.crm.createRecord("Leads",m);
  10. return "Lead created successfully ::: ";

File Content

The file types currently supported are the text files. In order to send the file to the function as a multipart data, send it under the argument name "inputFile".


To get the file uploaded to be used in the function:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. parameters = crmAPIRequestMap.get("file_content");
  3. company = parameters.get("Company");
  4. name = parameters.get("last_name");
  5. m = Map();
  6. m.put("Last_Name",name);
  7. m.put("Company",company);
  8. r = zoho.crm.createRecord("Leads",m);
  9. return "Lead created successfully ::: ";

User Information
To get the info about the users:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. // to get the user info of the request
  3. user_info = crmAPIRequestMap.get("user_info");
  4. /**
  5. Your Business Logic here
  6. **/
  7. return crmAPIRequestMap;

Authentication Type and Method

To get the info about the authentication type:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. // to get the HTTP method of the request
  3. user_info = crmAPIRequestMap.get("method");
  4. // to get the authentication type of the request
  5. user_info = crmAPIRequestMap.get("auth_type");
  6. /**
  7. Your Business Logic here
  8. **/
  9. return crmAPIRequestMap;

Headers

To get the headers of the request:
  1. crmAPIRequestMap = crmAPIRequest.toMap();
  2. // to get the user info of the request
  3. header_request = crmAPIRequestMap.get("headers");
  4. /**
  5. Your Business Logic here
  6. **/
  7. return crmAPIRequestMap;

Return Type

The default return type is String in Version 2.0. You can specify the return message as String or you can define response object by using crmAPIResponse(explained below).

Response Object

In the argument crmAPIResponse, you can define the following specifications about the response:

Status Code: Use this key to define the status of the API call (successful or bad request). The default status code is 200.
  1. response = Map();
  2. response.put("status_code",204);
  3. return {"crmAPIResponse":response};

Content-Type: Use this key to define the format in which you want the response. The default value of this key is application/json;charset=utf-8.
  1. response = Map();
  2. response.put("Content-Type","application/text");
  3. return {"crmAPIResponse":response};

Headers: Use this key to define the values in header. The default value of this key is {"Content-Disposition", "attachment;filename=response.json"}.
  1. response = Map();
  2. headers = Map();
  3. headers.put("X-ZOHO-SOURCE","CRM");
  4. headers.put("X-Frame-Options","SAMEORIGIN");
  5. headers.put("X-RATELIMIT-LIMIT","60");
  6. response.put("headers",headers);
  7. return {"crmAPIResponse":response};

Body: Use this key to pass the information that you need to send to the third-party as a response to their request. The default value of body is empty.
  1. response = Map();
  2. body = "{<xml>}"
  3. response.put("body",body);
  4. return {"crmAPIResponse":response};

Version 1.0

Request Object



Example
The following function code snippet calls the third-party "Twilio" REST API as a webhook and associates it with the application,
  1. //use this for v1.0
  2. twilioResp = (requestMap.get("parameters")).toMap();
  3. //use this for v2.0
  4. twilioResp = (crmAPIRequest.get("params")).toMap();
  5. //below lines are common for both the versions
  6. fromNumber = twilioResp.get("From");
  7. messageContent = twilioResp.get("Body");
  8. LeadRespList = zoho.crm.searchRecords("Leads", "(Mobile:equals:" + fromNumber + ")");
  9. for each LeadResp in LeadRespList
  10. {
  11. LeadId = LeadResp.get("id");
  12. if ((LeadId == null) || (LeadId == ""))
  13. {
  14. LeadId = "";
  15. }
  16. }
  17. ContactRespList = zoho.crm.searchRecords("Contacts", "(Mobile:equals:" + fromNumber + ")");
  18. for each ContactResp in ContactRespList
  19. {
  20. FinalId = ContactResp.get("id");
  21. if ((FinalId == null) || (FinalId == ""))
  22. {
  23. FinalId = "";
  24. }
  25. }
  26. updateMap = { ("twilioext.Incoming_Message_Content") : messageContent, "twilioext.SMS_Texts_Name" : ("Incoming SMS"), "twilioext.Lead" : LeadId, "twilioext.Contact" : FinalId, "twilioext.Direction" : "Inbound" };
  27. m = map();
  28. l = List();
  29. l.add(updateMap);
  30. m.put("module", "twilio.SMS_Texts");
  31. m.put("data", l);
  32. resp = zoho.crm.invokeConnector("crm.create", m);
  33. signalleadResp = zoho.crm.getRecordById("Leads", LeadId.toLong());
  34. Leademail = signalleadResp.get("Email");
  35. if ((Leademail != "") || (Leademail != null))
  36. {
  37. signalMap = map();
  38. signalMap.put("signal_namespace", ("twilioext.incomingsmssignal"));
  39. signalMap.put("email", Leademail);
  40. signalMap.put("subject", ("Incoming SMS"));
  41. signalMap.put("message", messageContent);
  42. actionsList = List();
  43. actionMap = map();
  44. actionMap.put("type", "link");
  45. actionMap.put("display_name", "View Email");
  46. actionMap.put("url", "www.google.com");
  47. actionsList.add(actionMap);
  48. signalMap.put("actions", actionsList);
  49. result = zoho.crm.invokeConnector("raisesignal", signalMap);
  50. info result;
  51. }
  52. signalcontactResp = zoho.crm.getRecordById("Contacts", FinalId.toLong());
  53. Contactemail = signalcontactResp.get("Email");
  54. if ((Contactemail != "") || (Contactemail != null))
  55. {
  56. signalMapx = map();
  57. signalMapx.put("signal_namespace", ("twilioext.incomingsmssignal"));
  58. signalMapx.put("email", Contactemail);
  59. signalMapx.put("subject", ("Incoming SMS"));
  60. signalMapx.put("message", messageContent);
  61. actionsListx = List();
  62. actionMapx = map();
  63. actionMapx.put("type", "link");
  64. actionMapx.put("display_name", "View Email");
  65. actionMapx.put("url", "www.google.com");
  66. actionsListx.add(actionMapx);
  67. signalMapx.put("actions", actionsListx);
  68. resultx = zoho.crm.invokeConnector("raisesignal", signalMapx);
  69. info resultx;
  70. }

Comparison of Version 1.0 and 2.0


Invoke REST API functions from widgets

You can invoke a REST API function from a  widget using the ZOHO.CRM.FUNCTIONS.execute() function. Using this function, you can pass user input values to the REST API function.

The API name of the function can be obtained from the Sandbox URL or Production URL (see below) that can be viewed by clicking  Invoke as REST API in the function's deluge script editor.

https://platform.zoho.com/crm/v2/functions/function_apiname/actions/execute?auth_type=apikey&zapikey=<zapikey>

  1. var func_name = "<function_apiname>";

  2. //obtain user inputs
  3. var lastName = $("#last-name").val();
  4. var company = $("#company").val();

  5. var req_data ={
  6. "arguments": JSON.stringify({
  7. "Last_Name" : lastName,
  8. "Company" : company
  9. })
  10. };
  11. ZOHO.CRM.FUNCTIONS.execute(func_name, req_data)
  12. .then(function(data){
  13.         console.log("REST API function invoked from widget."+data);
  14. })
Given below is the code snippet for the REST API function. Here, the user inputs from the widget is obtained through the params argument.
  1. // to get the parameters of the request
  2. params = crmAPIRequest.get("params");
  3. // to fetch the parameters from the arguments key
  4. args = params.get("arguments");
  5. /**
  6. Your Business Logic here
  7. **/
  8. return <function_response>;

    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

                                                                                                              • Custom Functions

                                                                                                                The guide will help you with the following: Create Custom Functions Program Custom Functions  Test Custom Functions  Associate with Workflow Rules  Manage Custom Functions  Custom functions help in automation where procedural logic is required, which ...
                                                                                                              • Automation - Setting up Custom Functions

                                                                                                                This guide will help you with the following: Create Custom Functions Program Custom Functions  Test Custom Functions  Associate with Workflow Rules  Manage Custom Functions  Custom functions help in automation where procedural logic is required, ...
                                                                                                              • Managing Profiles

                                                                                                                The profile function controls the permission to CRM modules, records, fields in a record, and various utilities, such as import, export, send email, mass update, etc. Users associated to the profile can access only the functions that are assigned to ...
                                                                                                              • Customization - Adding Custom Related Lists

                                                                                                                This guide will help you with the following: Add Custom Related Lists Create Custom Functions  Example  Edit Custom Related Lists   Delete Custom Related Lists  Each record created by your subscribers in your vertical CRM has the option to associate ...
                                                                                                              • Automation - Workflow Rules

                                                                                                                This guide will help you with the following: Create Workflow Rules Part 1 - Specify the basic details of the rule  Part 2 - Configure the execution criteria  Part 3 - Set the rule criteria  Part 4 - Associate instant and/or time based actions ...
                                                                                                                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