Custom Functions | Administrator Guide 5.0 | Zoho People help

Custom Functions Automation - Zoho People

What is a Custom Function?

Custom functions in Zoho People help simplify complex, multi-step actions through program scripts that you can write and execute. Custom functions helps in workflow automation where procedural logic is required, which cannot be implemented with the default actions such as, Alerts/Tasks/WebHooks, etc. With custom functions, you can automatically update the data in the related Zoho People records or third-party applications by executing simple program scripts.

Custom functions are written using Zoho's Deluge scripting language to effectively manipulate data, communicate with third-party applications and update responses. The script will run whenever a Workflow trigger events occurs.
Info
For more information about Deluge scripting language, refer to Zoho - Deluge Scripting Language.
Idea
Custom Function for a service can be created in Settings > Select a Service > Automation > Actions > Custom Functions.

How Custom Function works in Zoho People

  1. Create a Custom Function for a form in a service and associate it with a new or existing Workflow.
  2. Whenever a Workflow trigger event occurs, the Custom Function executes, performing the specified actions as defined in your code.

Create Custom Function

To create a custom function:
  1. From you home page, navigate to Settings > Select a Service > Automation > Custom Functions.

  2. Click Add Custom Function.
  3. Select the form for which you would like to create Custom Function.
  4. Enter a name for the Custom Function.
  5. Click Edit Parameters, define the method name, and specify the required method arguments by specifying the variable name and the corresponding field values.

  6. Click Save.
  7. Enter the description of the Custom Function.
  8. Enter the Deluge code, for more information, refer to Zoho - Deluge Scripting Language
    1. For Syntax auto-suggestion, enable Syntax Builder.
    2. For Connections, refer to Connections.
  9. Click Save.
    Idea
    Click Save and Execute to test and run the script immediately.
Info
Map the created custom function to a workflow to automatically run the code whenever a trigger event defined in the workflow occurs.

Map a Custom Function to Workflow

To Map a Custom Function to a Workflow:
  1. From your home page, go to Settings > Select a Service > Workflows > Add Workflow.
  2. Provide all the required details, Learn more to configure Workflow.
  3. Under Actions, click Custom Functions and click the (+) add icon to add a new custom function to the workflow or you can select the existing custom function which are created for the form.

  4. Click Add.
    A workflow is created and based on the trigger events the custom function executes, performing the specified actions as defined in your code.

Use Cases

Automatic travel expense update using custom functions

Consider a scenario of calculating an employee's travel expenses. All of the travel expenses gets automatically calculated using the below custom function in the travel expense form. Thus, the respective employee's self-service form will get the updated travel expense details. To achieve this, a custom field has to be created in the employee self-service form.
  1. From your home page, go to Settings > Select a Service (Travel) > Automation > Actions > Custom Functions.
  2. Select respective Form (Travel Expense) and click  Add Custom Function.
  3. Provide a Custom Function Name and required parameter details
  4. Provide the script as given below.
  5. Click Save  to save the Custom Function.
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. travelExpenseMap = zoho.people.getRecordById("travelexpenses",travel_id);
  2. tabularList = travelExpenseMap.get("tabularSections").get("Expense");
  3. totalExp = 0;
  4. for each str in tabularList
  5. {
  6. totalExp = totalExp + str.get("Ticket").toDecimal() + str.get("Lodging").toDecimal() + str.get("LocalConveyance").toDecimal() + str.get("Boarding").toDecimal() + str.get("Others").toDecimal() + str.get("Incidentals").toDecimal() +str.get("Phone").toDecimal();
  7. }
  8. updateemp = Map();
  9. updateemp.put("recordid",erecno);
  10. updateemp.put("Total_Expense",totalExp);
  11. updaterecord = zoho.people.update("employee",updateemp);
  12. info updaterecord;

Automatically change the employee status from active to resigned during an employee's date of exit using Custom Functions

An employee is resigning his job and you want to change the status to 'Resigned' in the employee form during the date of exit. You can do this action using the below script in our custom function. By adding exit details for an employee, a workflow will be triggered where the mapped custom function will get executed. Upon successful execution, the selected employee's employee status value will be changed to 'Resigned'. Thus, the employee cannot log in to the organization account anymore.
  1. From your home page, go to Settings > Select a Service > Automation > Actions > Custom Functions.
  2. Select respective Form and click  Add Custom Function.
  3. Provide a Custom Function Name and required parameter details
  4. Provide the script as given below.
  5. Click Save  to save the Custom Function
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. getrecord = zoho.people.getRecordById("employee",erecno);
  2. params = Map();
  3. params.put("scope","creatorapi");
  4. params.put("Employee_ID",empid);
  5. params.put("First_Name",fname);
  6. params.put("Last_Name",lname);
  7. params.put("Email_ID",emailid);
  8. pushrecord = invokeurl
  9. [
  10.   url: "https://creator.zoho.com/api/karenjohn/json/custom-function/form/Custom_form/record/add/"
  11.     type: POST
  12.     parameters: params
  13.     connection : "cf"
  14. ];
  15. info pushrecord;

Automatic check out in Attendance module

Let us consider a scenario, where an employee forgets to do check out on the system at the end of the day. It is possible to use Custom Functions to do an Auto-check out on the system. This helps in tracking the exact working hours of an employee.

Use the following parameters for both the methods mentioned below.



Method 1: When the check out is not done, Custom Functions can be used to update the respective shift's end time as the check out time on the system.
Steps:
  1. From your home page, go to Settings > Select a Service > Automation > Actions > Custom Functions.
  2. Select respective Form and click  Add Custom Function.
  3. Provide a Custom Function Name and required parameter details
  4. Provide the script as given below.
  5. Click Save to save the Custom Function
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. getUA = Collection();
  2. getUA.insert("erecno":erecno);
  3. get_Rec = invokeurl
  4. [
  5. url :"https://people.zoho.com/people/api/attendance/getUserAvailability"
  6. type :POST
  7. parameters:getUA.toMap()
  8. connection : "peoplecf"
  9. ];
  10. isUserAvailable = get_Rec.get("isUserAvailable");
  11. if(isUserAvailable == true)
  12. {
  13. getdata = zoho.people.getRecordByID("P_AttendanceForm",recordid.toLong());
  14. expcheckout = getdata.get("ExpectedToTime");
  15. addrecord = Map();
  16. addrecord.put("empId",empid);
  17. addrecord.put("checkOut",expcheckout);
  18. updaterecord = invokeurl
  19. [
  20.     url: "https://people.zoho.com/people/api/attendance"
  21.     type: POST
  22.     parameters: addrecord
  23.     connection : "peoplecf"
  24. ];
  25. info updaterecord;
  26. }
Method 2:  When the check out is not done,"08" hours from check-in time can be updated as check out time on the system using Custom Functions.
  1. From your home page, navigate to Settings > Select a Service > Automation > Actions > Custom Functions.
  2. Select respective Form and click  Add Custom Function.
  3. Provide a Custom Function Name and required parameter details
  4. Provide the script as given below.
  5. Click Save to save the Custom Function.
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. getUA = Collection();
  2. getUA.insert("erecno":erecno);
  3. get_Rec = invokeurl
  4. [
  5. url :"https://people.zoho.com/people/api/attendance/getUserAvailability"
  6. type :POST
  7. parameters:getUA.toMap()
  8. connection : "peoplecf"
  9. ];
  10. isUserAvailable = get_Rec.get("isUserAvailable");
  11. if(isUserAvailable == true)
  12. {
  13. getdata = zoho.people.getRecordByID("P_AttendanceForm",recordid.toLong());
  14. checkin = getdata.get("FromTime").toTime();
  15. addedhrs= checkin.addHour(8);
  16. addrecord = Map();
  17. addrecord.put("empId",empid);
  18. addrecord.put("checkOut",addedhrs);
  19. updaterecord = invokeurl
  20. [
  21.     url: "https://people.zoho.com/people/api/attendance"
  22.     type: POST
  23.     parameters: addrecord
  24.     connection : "peoplecf"
  25. ];
  26. info updaterecord;
  27. }
Info
Automatic check-out custom functions can be used only when all employees in the organization belong to the same time zone.

Automatic conversion of any currency to USD

Irrespective of the amount in the travel claim form, the currency will be converted to USD based on the exchange rate of the day on which the claim is raised.
  1. From your home page, navigate to Settings > Select a Service (Travel) > Automation > Actions > Custom Functions.
  2. Select respective Form (Travel Claim Form) and click  Add Custom Function
  3. Provide a Custom Function Name and required parameter details.
    Info
    Parameters:
    recordId = ZohoID(Travel Claim) 
  4. Provide the script as given below.
  5. Click Save to save the Custom Function.
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. getValues = zoho.people.getRecordById("Travel_Claim",recordId);
  2. tabularsec = getValues.get("tabularSections").get("Cost Details");
  3. result = 0;
  4. for each tabularsecvalues in tabularsec.toList()
  5. {
  6. cost = tabularsecvalues.get("");
  7. format = tabularsecvalues.get("");
  8. if(format != "USD" && cost > 0.00 && format != "")
  9. {
  10. currencyvalues = getUrl("https://data.fixer.io/api/latest?access_key=YOUR_ACCESS_KEY&base=USD");
  11. formatvalues = currencyvalues.get("rates").get(format);
  12. totalval = cost.toDecimal() * formatvalues.toDecimal();
  13. total = totalval.round(2);
  14. result = total + result;
  15. }
  16. }
  17. addrecmap = Map();
  18. addrecmap.put("recordid",recordId);
  19. addrecmap.put("total",result.toString());
  20. addtotal = zoho.people.update("Travel_Claim",addrecmap);
  21. info addtotal;
Idea
Navigate to Settings > Developer Space > Zoho People API tab to know more about form and field link names.

Adding leave balance

If leave balance has to be given to an employee, the below code can be used. leave type ID of a particular leave type can be obtained from this API and can be replaced in the code.
  1. From your home page, navigate to Settings > Select a Service (Leave) > Automation > Actions > Custom Functions.
  2. Select respective Form (Leave) and click  Add Custom Function
  3. Provide a Custom Function Name and required parameter details.
    Info
    Parameters:
    erecno = ID (Employee ID)
    count = count (Leave credit)
  4. Provide the script as given below.
  5. Click Save to save the Custom Function.
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. param = Collection();
  2. leavetypeid = "494174000000515325";
  3. details = "{" + erecno + ":{" + leavetypeid + ":{'date':" + today.toString("dd-MMM-yyyy") + ",'count':" + count + "}}}";
  4. param.insert("balanceData":details);
  5. getRecord = invokeurl
  6. [
  7.     url :"https://people.zoho.com/people/api/leave/addBalance"
  8.     type :POST
  9.     parameters:param.toMap()
  10.     connection:"people_cf"
  11. ];
  12. info getRecord;

Send email if total hours for a week is less than 45 hours for an employee

By making use of the below script, if total hours of attendance is less than 45 hours for an employee, then an email can be sent.
  1. From your home page, navigate to Settings > Select a Service (Leave) > Automation > Actions > Custom Functions.
  2. Select respective Form (Leave) and click  Add Custom Function
  3. Provide a Custom Function Name and required parameter details.
    Info
    Parameters:
    erecno = ID (Employee ID)
    count = count (Leave credit)
  4. Provide the script as given below.
  5. Click Save to save the Custom Function.
    Idea
    Click Save and Execute to test and run the script immediately.
Quote
  1. start_date = today.toDate().subDay(7);
  2. end_date = today.toDate().subDay(1);
  3. getrec_Col = Collection();
  4. getrec_Col.insert("startDate":Start_Date);
  5. getrec_Col.insert("endDate":End_Date);
  6. getrec_Col.insert("dateFormat":"dd-MMM-yyyy");
  7. get_rec = invokeurl
  8. [
  9. url :"https://people.zoho.com/people/api/attendance/getSummaryReport"
  10. type :POST
  11. parameters:getrec_Col.toMap()
  12. connection : "cf"
  13. ];
  14. summary_Reports = get_rec.get("summaryReport");
  15. for each  summary in summary_Reports
  16. {
  17. emailId = summary.get("emailId");
  18. totalWorkedDays = summary.get("totalHours");
  19. totalWorkedDays = totalWorkedDays.getPrefix(":").toNumber();
  20. if(totalWorkedDays < 45)
  21. {
  22.     sendmail
  23.     [
  24.         from : zoho.adminuserid
  25.         to : emailId
  26.         subject: "Total Hours"
  27.         message : "<div>Hi,<br></div><div><br></div><div>You have not completed 45 hours for the previous week.<br></div><div><br></div><div>Thanks,<br></div><div>Admin Team</div>"
  28.     ]
  29. }
  30. }

IdeaNavigate to Settings > Developer Space > Zoho People API tab to know more about form and field link names.
Info
The above scripts can also be edited and used to serve other similar scenarios.
Info
All APIs used in Custom Functions have threshhold limits. Refer the API guide for the limits under each module and customize the code accordingly.