Zoho FSM | Standalone Functions

Standalone Functions

Field service management will be at its optimal level when there is proper synergy between all the people and processes involved. Crucial data conveyed on time to the correct stakeholder is paramount in maintaining the KPIs. To this end, Zoho FSM provides Standalone Functions that will help you in processing data in FSM when it is driven by external parameters. You can write functions that will cater to complex business requirements and expose the function as a REST API. Thus a standalone function can be used by Zoho FSM, other Zoho applications, and any third-party application.

Let's better understand the use of the standalone functions using an example. Consider a company Zylker Inc., that is in the business of selling and managing vending machines. They require information about the real-time stocks in vending machines in different locations so that they can restock on time. The IOT-enabled vending machines will be able to transmit such information through smart sensors. Zylker Inc. uses Zoho FSM and stores details of its vending machines in the Assets module. Each asset will have a boolean field called Restock Required. When the stock in an asset reaches the restock level, the value of Restock Required can be updated to true using a standalone function. Using this action as a trigger, a workflow can create a request to restock the asset. Thus, with minimal human intervention, and interaction, servicing for the vending machine is done proactively.


 Available in Editions: StandardProfessionalPremium


Info
Permission Required:
  1. For creating: Manage Automation
  2. For executing: Function as REST API
- Find out the Edition-specific limits for Standalone Functions.

Supported Authorization Types
OAuth 2.0

Supported Request methods

Method

Scope

GET

ZohoFSM.functions.execute.READ

POST

ZohoFSM.functions.execute.CREATE


Function Parameter
The function has a Map parameter APIRequest. When you make the function call, APIRequest will contain the following details:
 

Name

Description

Body
Any input that is passed as a stream to the request can be fetched using the component body of APIRequest.
 
body = APIRequest.get("body");
Parameters
The parameter values passed in the request can be fetched using the component params of APIRequest.

If the parameter value is a key-value pair, then use:
parameters = APIRequest.get("params").get(<key>);

If the parameter value is a JSON object, name the key as arguments, and use:
parameters = APIRequest.get("params").get("arguments").get(<key>);

File Content
Any multipart content like a file can be fetched using the component file_content of APIRequest.
 
fileContent = APIRequest.get("file_content");
 
Maximum allowed file size is 4MB.
Supported file types: .txt, .csv, .xml, .json
User Information
Details about the user can be fetched using the component user_info of APIRequest.
 
userInfo = APIRequest.get("user_info");
Authentication Method
The name of the HTTP method (GET, POST, etc.) used in the request can be fetched using the component method of APIRequest.
 
method = APIRequest.get("method");
Authentication Type
The name of the authentication method used in the request can be fetched using the component auth_type of APIRequest.
 
authType = APIRequest.get("auth_type");
Headers
The information that is available in header (e.g. additional information about the request) can be fetched using the component headers of APIRequest.
 
headers = APIRequest.get("headers");
 
Return Type  
The return type of the function is String.

Create a Standalone Function

Following are the steps to create a standalone function that updates a custom field Restock Required of type Boolean in the Assets module to true:
  1. Navigate to Setup > Developer Space > Standalone Functions and click Create Function.



  2. In the Create Function overlay, do the following:
    1. Enter a Function Name
    2. In the Deluge Script Editor, write the below script for the standalone function.

      newRecordInfo = Map();
      newRecordInfo.put("Restock_Required__C",true);
      //Restock_Required__C is the API name of the custom field
      resp = zoho.fsm.updateRecord("Assets",APIRequest.get("params").get("id"),newRecordInfo);
      return resp;

  3. Click Save.


To test the function, do the following:
  1. In the Deluge Script Editor, click Save & Execute.
  2. In the Execute Function pop-up, enter the values to be passed to the map argument inputData. Click Execute.

    Sample argument:
    {"id":"2819XXXX0015"}



    The results of the function will be displayed in the console. You can use this to test and debug your custom functions.


Once the function is created, toggle the status of the function to active and the REST API URL of the function will be displayed. Use this REST API URL to call this function. Click the copy [] icon to copy the URL.

Sample API URL: https://fsm.zoho.com/fsm/v1/functions/restockupdate/actions/execute?auth_type=oauth


Test in Postman

You can test the standalone function in Postman. To do so:
  1. Create a request to connect to the standalone function REST API. Use the following:
    a. Request method: POST
    b. Authorization: OAuth 2.0
    c. Callback URL: https://oauth.pstmn.io/v1/callback
    d. Auth URL: https://accounts.zoho.com/oauth/v2/auth
    e. Access Token URL: https://accounts.zoho.com/oauth/v2/token
    f. Scope: ZohoFSM.settings.functions.CREATE
    g. Generated Client IDClient Secret


When successfully executed, the value of the field Restock Required in the Assets module will be updated to true.


Types of Inputs

This section illustrates how different types of inputs can be passed to the standalone function.
  1. Execute the function using the GET method and pass arguments to the params component of APIRequest.



  2. Execute the function using the POST method and pass arguments to the params component of APIRequest.



  3. Execute the function using the POST method and pass value to the body component of APIRequest.



  4. Execute the function using the POST method and passing arguments to the file_content component of APIRequest.

    fileContent = APIRequest.get("file_content");
    myfile = fileContent.tofile("sf.csv");

    headers = APIRequest.get("headers");
    body = APIRequest.get("body");
    auth_type = APIRequest.get("auth_type");
    params = APIRequest.get("params");
    uinfo = APIRequest.get("user_info");
    method = APIRequest.get("method");

    info "File Content: " + fileContent;
    info "Headers: " + headers;
    info "Body: " + body;
    info "Auth Type: " + auth_type;
    info "Params: " + params;
    info "User Info: " + uinfo;
    info "Method: " + method;

    sendmail
    [
    from :zoho.adminuserid
    to :zoho.adminuserid
    subject :"Testing Standalone Function with File Attachment"
    message :APIRequest
    Attachments :file:myfile
    ]

    return APIRequest;

    The info messages will be available in the userMessage key and the return value of the function will be available in the output key.



Invoke Standalone Function within a Function

You can invoke a standalone function from another function in Zoho FSM, other Zoho applications, and any third-party application. Given below is a sample script for invoking a standalone function from another function.

rest = invokeurl
[
    url :"https://fsm.zoho.com/fsm/v1/functions/createcontact1/actions/execute?auth_type=oauth"
    type :GET
    parameters:{"Last_Name":"Robins","Email":"lucy.robins@zylker.com"}
    headers:Map()
    connection:"fsm"
];

return rest;

Manage Standalone Functions 

You can edit, delete, or clone a standalone function.