Overview - Serverless Functions

Overview - Serverless Functions

Serverless architecture, also known as “Function-as-a-service”(Faas), provides a platform for developers to execute their own codes in response of various business events. In Zoho CRM, all these codes can be written through deluge scripts and can be run without provisioning or managing servers. These codes once written can be triggered from any other third party services or it can be called directly triggered from any web or mobile app.

Though it is called “Serverless”, it requires servers to run code. So, the word “Serverless” defines that the developers or organizations need not buy or provision or rent any servers to run their code.

Calling Functions using REST APIs

A function lets you have additional functionalities and features in your CRM apart from the conventional features. Functions needs a trigger to call it. The trigger could be in the form of a workflow, blueprint, related list or through the click of a button.

If the function needs to be triggered without employing these methods or from external sources, you can create standalone functions and make them available as API calls. Triggering functions through REST APIs which gives the flexibility to trigger it from anywhere inside Zoho CRM or from any third party application.

You can call functions as an API in two ways:
  • OAuth2
  • API Key
Apart from using functions as APIs, you can also use functions within other functions. This is made possible since the entire function is reduced to a single line of code.

However, there are some precautions that you need to take when calling a function in another function. The following is a sample code snippet which denotes a function called within another function.

Function within a function(using APIs)

  1. URL=<REST API Function URL>;
  2. param = Map();
  3. param.put(<argument-name>, <argument-value>);
  4. param.put(<argument-name>, <argument-value>);
  5. param.put(<argument-name>, <argument-value>);
  6. argument = Map();
  7. argument.put("arguments", param);
  8. response = invokeurl
  9. [
  10. url: URL
  11. type: GET | POST
  12. parameters: argument
  13. headers: {}
  14. ];
  15. return response;

Functions in Postman

Functions which are given as API calls can be tested using POSTMAN application. However, there are a few things that you need to keep in mind when executing a function in that app.


Argument:
  1. arguments=
  2. {
  3. "emailAddress":"abc@gmail.com",
  4. "extraDetails":{
  5. "name":"Abc",
  6. "signature":"XYZ"
  7. }
  8. }
Note

Please avoid using the name "arguments" as an argument within the function. This is because all of the arguments passed inside the JSON will be mapped to the single "arguments" and the others will be left empty.

Request URL

HTTP Methods

  • GET
  • POST
The arguments given below are in encoded format.

You cannot pass the arguments as a JSON object within the request URL. Please encode the arguments in case they are to be given within the request URL.
  1. arguments=%7B%0D%0A%09"emailAddress"%3A"abc@gmail.com"%2C%0D%0A%09"extraDetails"%3A%7B%0D%0A%09%09"name"%3A"Abc"%2C%0D%0A%09%09"signature"%3A"XYZ"%0D%0A%09%7D%0D%0A%7D%0D%0A
You can call the arguments as a Header in the API. For instance, URL would be "{Rest API URL}?age=15&name=Robert", where "age" and "name" are arguments in the function.

Additionally, for any extra arguments that are not defined in the function, but used in the API, normally an error would be thrown. However, you can preemptively add an extra argument in the function under the name "CRM API Request". This argument would contain or take in all the extra arguments that you pass within the API call.


Request Body

In the Body > form-data section, create a key with the name "arguments".
  1. arguments={"emailAddress":"abc@gmail.com","extraDetails":{"name":"Abc","signature":"XYZ"}}


Note

POST request - Arguments Limit
  • In Request URL - 5000 lines.
  • In Body - 95000 lines (as JSON object).

Calling a Function within another Function

Often, there might be some requirements where functions needed to be called within other functions. Just like how you call a function within Workflows, you can call them within other functions. Let us consider two functions, A and B.

In order to pass the arguments(parameters) from function A to B, they would have to be passed either in the Query URL String or in form-data. In order to achieve that, please make use of the snippet given below within your function.



The above code snippet showcases a sample where the arguments from function A are called as parameters in the . You need to create a list with the name 'file' and decide how the params of the function A will be made use of in function B.

The headers of the function is form-data and the 'file' needs to be called.

You can directly copy the below code snippet within your function.

  1. param = Map();
  2. param.put("{key}","{value}");
  3. param.put("{key}","{value}");
  4. param.put("{key}","{value}");
  5. files = list();
  6. for each key in param.keys()
  7. {
  8. stringpart = Map();
  9. stringpart.put("stringPart","true");
  10. stringpart.put("content","" + param.get(key));
  11. stringpart.put("paramName",key);
  12. files.add(stringpart);
  13. }
  14. response = invokeurl
  15. [
  16. url :"{Function_API_URL}"
  17. type :POST
  18. headers:{"Content-Type":"multipart/form-data"}
  19. files:files
  20. ];

    • Related Articles

    • Authentication - Serverless Functions

      Authentication Method A serverless function within your CRM can be invoked from any third-party application or within the CRM. However, commonly not all of the applications support a single authentication method. Keeping that in mind, we have two ...
    • Using API Key - Serverless Functions

      Introduction A severless function within your CRM can be invoked from any third-party application or within the CRM using a webhook. However, generally most of the webhooks do not support OAuth2, save for a few of them. In that case, you can execute ...
    • Types of Inputs - Serverless Functions

      The input for the function can be acquired in the following types Parameters Parameters are passed in URL of query string and the data passed in the post "form-data" section in an input. The "params" in the crmAPIRequest map contains these parameters ...
    • Using OAuth2 - Serverless Functions

      Introduction Functions can be made accessible through OAuth2 protocol. OAuth2 method allows you to share specific data with any application while keeping your usernames and passwords private, by having specific scopes which grant access to specfic ...
    • Request and Response Object - Serverless Functions

      Request object You can get the entire Request Object within the function using the "crmAPIRequest" argument. Say you've created a function and defined 2 arguments. Now you need to use the same function in 2 different webhooks, each of which might ...