This guide helps you with the following:
- Create Functions
- Normal Functions
- REST API Functions
- Invoke Functions
- Edit Functions
- Delete Functions
- Rest API Functions
- Authentication
- Version 2.0
- Version 1.0
- Example
- Comparison of Version 2.0 and 1.0
- 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
- Single or multiple arguments of supported data types can be passed as parameters to the functions.
- Functions can return values with one of the supported data types.
- 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,
- Log in to the Zoho Developer Console and click Extensions for Zoho CRM.
- Select the extension you want to create a function for, and click Edit.
- Click Automate in the left pane, then click Functions.
- Click Create Function in Functions page.
- In the Create Function page, fill in the following details,
- Name of the function
- Invoke as REST API - "No" for normal functions and "Yes" for REST API functions.
- Select the return type of the function
- Click Add Arguments if any,and specify the arg name and type
- Click Save.
A sample code snippet in Deluge Script for a normal function is given below,
- string pluginname.Copy_Address(string module, int id)
- {
- m = { "module" : input.module, "id" : input.id };
- resp = zoho.crm.invokeConnector(("crm.get"), m);
- response = (resp.get("response")).toMap();
- data = response.get("data").toJSONList();
- respstr = "";
- for each rec in data
- {
- dataMap = rec.toMap();
- idd = dataMap.get("id");
- street = dataMap.get("Street");
- city = dataMap.get("City");
- state = dataMap.get("State");
- country = dataMap.get("Country");
- zip = dataMap.get("Zip_Code");
- updateDataMap = map();
- updateDataMap.put("installscript.Billing_Street", street);
- updateDataMap.put("installscript.Billing_City", city);
- updateDataMap.put("installscript.Billing_State", state);
- updateDataMap.put("installscript.Billing_ZipCode", zip);
- updateDataMap.put("installscript.Billing_Country", country);
- updateDataMap.put("id", idd);
- l = List();
- l.add(updateDataMap);
- updMap = { "module" : input.module, "data" : l };
- updateResp = zoho.crm.invokeConnector("crm.update", updMap);
- respstr = updateResp+"";
- }
- return respstr;
- }
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,
- resp = <EXTENSIONNAME>.Copy_Address("Leads", <LEADID>);
The "Copy_Address" function generates output API response as,
- {"response":"{\"data\":[{\"code\":\"SUCCESS\",\"details\":
- {\"Modified_Time\":\"2017-08-29T14:53:08+05:30\",\"Modified_By\":
- {\"name\":\"Saranya \",\"id\":\"2641973000000124009\"},
- \"Created_Time\":\"2017-08-29T14:28:16+05:30\",
- \"id\":\"2641973000000128184\",\"Created_By\":{\"name\":\"Saranya \",
- \"id\":\"2641973000000124009\"}},\"message\":\"record
- 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
- Log in to your Zoho Developer Console and select Extensions for Zoho CRM.
- Select the extension your function is associated with, and click Edit.
- Click Automate option in the left pane, then click Functions.
- Select the required function from the list and click the Edit icon [].
- Edit and update the changes as per your requirements.
Delete Functions
To delete a function
- Log in to your Zoho Developer Console account and select Extensions for Zoho CRM.
- Select the extension associated with the function you wish to delete, and click Delete.
- Click Automate option in the left pane and click Functions.
- Choose the function that you want to delete, and click the Delete icon.
- Calling a standalone function inside another standalone function is not supported.
- 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:
- Custom Functions
- Installation scripts
Using Custom Functions
- Publish the extension in which the REST API Function is included and install the extension.
- Log in to your application.
- Go to Setup > Automation > Actions and select the Functions tab.
- Click the Configure Function button.
- Click the Write your own link.
- In the Deluge Script Editor, paste the following code snippet and click Save & Execute Script.
- m = {"nameSpace" : "<portal_name.extension_namespace>"};
- apikeyresp = zoho.crm.invokeConnector("crm.zapikey",m);
- zapikey = apikeyresp.get("response");
- info zapikey;
7. The ZAPI key will be displayed in the Log messages.
Using Installation Scripts
- Log in to your Zoho Developer console and click Extensions for Zoho CRM.
- Select the extension in which the REST API Function is included, then click Edit.
- Select Custom Properties in the left pane.
- Click the Create button.
- 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.
- m = { "nameSpace" : "<portal_name.extension_namespace>" };
- apikeyresp = zoho.crm.invokeConnector("crm.zapikey", m);
- zapikey = apikeyresp.get("response");
- url = "https://platform.zoho.com/crm/v2/settings/custom_functions/<extension_namespace.
- function_name>/execute?zapikey=" + zapikey;
- m = { "apiname" : "<extension_namespace.variable_name>", "value" : url };
- r = zoho.crm.invokeConnector("crm.set", m);
- 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.
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:
- crmAPIRequestMap = crmAPIRequest.toMap();
- request_body = crmAPIRequestMap.get("body");
- company = request_body.get("Company");
- name = request_body.get("last_name");
- m = Map();
- m.put("Last_Name",name);
- m.put("Company",company);
- r = zoho.crm.createRecord("Leads",m);
- 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:
- crmAPIRequestMap = crmAPIRequest.toMap();
- parameters = crmAPIRequestMap.get("params");
- company = parameters.get("Company");
- name = parameters.get("last_Name");
- m = Map();
- m.put("Last_Name",name);
- m.put("Company",company);
- r = zoho.crm.createRecord("Leads",m);
- return "Lead created successfully ::: ";
Sending parameters using POSTMAN, as JSON objects in form-data:
To get the additional parameters used in the request:
- crmAPIRequestMap = crmAPIRequest.toMap();
- parameters = crmAPIRequestMap.get("params");
- args = parameters.get("args");
- company = args.get("Company");
- name = args.get("last_name");
- m = Map();
- m.put("Last_Name",name);
- m.put("Company",company);
- r = zoho.crm.createRecord("Leads",m);
- 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:
- crmAPIRequestMap = crmAPIRequest.toMap();
- parameters = crmAPIRequestMap.get("file_content");
- company = parameters.get("Company");
- name = parameters.get("last_name");
- m = Map();
- m.put("Last_Name",name);
- m.put("Company",company);
- r = zoho.crm.createRecord("Leads",m);
- return "Lead created successfully ::: ";
User Information
To get the info about the users:
- crmAPIRequestMap = crmAPIRequest.toMap();
- // to get the user info of the request
- user_info = crmAPIRequestMap.get("user_info");
- /**
- Your Business Logic here
- **/
- return crmAPIRequestMap;
Authentication Type and Method
To get the info about the authentication type:
- crmAPIRequestMap = crmAPIRequest.toMap();
- // to get the HTTP method of the request
- user_info = crmAPIRequestMap.get("method");
- // to get the authentication type of the request
- user_info = crmAPIRequestMap.get("auth_type");
- /**
- Your Business Logic here
- **/
- return crmAPIRequestMap;
To get the headers of the request:
- crmAPIRequestMap = crmAPIRequest.toMap();
- // to get the user info of the request
- header_request = crmAPIRequestMap.get("headers");
- /**
- Your Business Logic here
- **/
- 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.
- response = Map();
- response.put("status_code",204);
- 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.
- response = Map();
- response.put("Content-Type","application/text");
- 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"}.
- response = Map();
- headers = Map();
- headers.put("X-ZOHO-SOURCE","CRM");
- headers.put("X-Frame-Options","SAMEORIGIN");
- headers.put("X-RATELIMIT-LIMIT","60");
- response.put("headers",headers);
- 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.
- response = Map();
- body = "{<xml>}"
- response.put("body",body);
- 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,
- //use this for v1.0
- twilioResp = (requestMap.get("parameters")).toMap();
- //use this for v2.0
- twilioResp = (crmAPIRequest.get("params")).toMap();
- //below lines are common for both the versions
- fromNumber = twilioResp.get("From");
- messageContent = twilioResp.get("Body");
- LeadRespList = zoho.crm.searchRecords("Leads", "(Mobile:equals:" + fromNumber + ")");
- for each LeadResp in LeadRespList
- {
- LeadId = LeadResp.get("id");
- if ((LeadId == null) || (LeadId == ""))
- {
- LeadId = "";
- }
- }
- ContactRespList = zoho.crm.searchRecords("Contacts", "(Mobile:equals:" + fromNumber + ")");
- for each ContactResp in ContactRespList
- {
- FinalId = ContactResp.get("id");
- if ((FinalId == null) || (FinalId == ""))
- {
- FinalId = "";
- }
- }
- updateMap = { ("twilioext.Incoming_Message_Content") : messageContent, "twilioext.SMS_Texts_Name" : ("Incoming SMS"), "twilioext.Lead" : LeadId, "twilioext.Contact" : FinalId, "twilioext.Direction" : "Inbound" };
- m = map();
- l = List();
- l.add(updateMap);
- m.put("module", "twilio.SMS_Texts");
- m.put("data", l);
- resp = zoho.crm.invokeConnector("crm.create", m);
- signalleadResp = zoho.crm.getRecordById("Leads", LeadId.toLong());
- Leademail = signalleadResp.get("Email");
- if ((Leademail != "") || (Leademail != null))
- {
- signalMap = map();
- signalMap.put("signal_namespace", ("twilioext.incomingsmssignal"));
- signalMap.put("email", Leademail);
- signalMap.put("subject", ("Incoming SMS"));
- signalMap.put("message", messageContent);
- actionsList = List();
- actionMap = map();
- actionMap.put("type", "link");
- actionMap.put("display_name", "View Email");
- actionMap.put("url", "www.google.com");
- actionsList.add(actionMap);
- signalMap.put("actions", actionsList);
- result = zoho.crm.invokeConnector("raisesignal", signalMap);
- info result;
- }
- signalcontactResp = zoho.crm.getRecordById("Contacts", FinalId.toLong());
- Contactemail = signalcontactResp.get("Email");
- if ((Contactemail != "") || (Contactemail != null))
- {
- signalMapx = map();
- signalMapx.put("signal_namespace", ("twilioext.incomingsmssignal"));
- signalMapx.put("email", Contactemail);
- signalMapx.put("subject", ("Incoming SMS"));
- signalMapx.put("message", messageContent);
- actionsListx = List();
- actionMapx = map();
- actionMapx.put("type", "link");
- actionMapx.put("display_name", "View Email");
- actionMapx.put("url", "www.google.com");
- actionsListx.add(actionMapx);
- signalMapx.put("actions", actionsListx);
- resultx = zoho.crm.invokeConnector("raisesignal", signalMapx);
- info resultx;
- }
Comparison of Version 1.0 and 2.0
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>
- var func_name = "<function_apiname>";
- //obtain user inputs
- var lastName = $("#last-name").val();
- var company = $("#company").val();
- var req_data ={
- "arguments": JSON.stringify({
- "Last_Name" : lastName,
- "Company" : company
- })
- };
- ZOHO.CRM.FUNCTIONS.execute(func_name, req_data)
- .then(function(data){
- console.log("REST API function invoked from widget."+data);
- })
Given below is the code snippet for the REST API function. Here, the user inputs from the widget is obtained through the params argument.
- // to get the parameters of the request
- params = crmAPIRequest.get("params");
- // to fetch the parameters from the arguments key
- args = params.get("arguments");
- /**
- Your Business Logic here
- **/
- return <function_response>;