Automation-Custom Functions

Custom Functions

Purpose

Custom Functions lets you create user-defined functions that can be reused anywhere in Zoho People. You can maintain and edit custom functions and the script will be in one central location. These functions are mainly created to manipulate data, connect with a third party and internal applications and update responses in Zoho People.

Creating a Custom Function

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select Form Name
  • Provide Function Name and click Edit arguments
  • Provide Method Name, under Method Arguments, Specify customer value or Show people field
  • Click Save
  • Provide the script and click Save & Execute Script
  • Execute

The executed message will be displayed and if not, please click Execute Again to retry the execution

Mapping a Custom Function to Workflow

  • From your home page, go to Settings > Workflow under Automation > Add Workflow
  • Provide all the required details. Learn more to configure a workflow
  • Under Action, click Custom Functions and click either the add icon to add the custom function to the workflow or the folder icon to map the custom function

  • Click Save

Example scenario - 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.

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name
  • Provide Function Name
  • Provide the script as given below:

travelExpenseMap = zoho.people.getRecordById("travelexpenses",travel_id);

travelId = travelExpenseMap.get("TravelID.ID");

travelIdStr = travelExpenseMap.get("TravelID");

tabularMap = Map();

tabularMap = travelExpenseMap.get("tabularSections").toMap();

tabularList = List();

tabularList = tabularMap.get("Expense");

travelExpMap = Map();

totalExp = 0;

for each str in tabularList { travelExpMap = str.toMap();

totalExp = totalExp + travelExpMap.get("Ticket").toDecimal() + travelExpMap.get("Lodging").toDecimal() + travelExpMap.get("LocalConveyance").toDecimal() + travelExpMap.get("Boarding").toDecimal() + travelExpMap.get("Others").toDecimal() + travelExpMap.get("Incidentals").toDecimal() + travelExpMap.get("Phone").toDecimal(); } info totalExp;

travelReqUpdMap = Map();

travelReqUpdMap.put("recordid",travelId.toString());

erecno = travelExpenseMap.get("EmployeeID.ID");

updateemp = Map();

updaterecord = Map();

updateemp.put("recordid",erecno);

updateemp.put("Total_Expense",totalExp.toString());

updaterecord = zoho.people.update("employee",updateemp);

info updaterecord;

 

Example scenario -automatic data syn with creator using custom functions

To edit an employee's record of your organization, their employee ID, first name, last name, email ID will get automatically added in the respective creator form using the below-specified script.

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name
  • Provide Function Name
  • Provide the script as given below:
     

    record = Map();

    updaterecord = Map();

    record.put("recordid",erecno);

    record.put("Employeestatus",'Resigned');

    updaterecord = zoho.people.update("employee",record);

    info updaterecord;

  • Click Save & Execute Script

Example scenario-automatically change the employee status from active to resign during an employee's date of exit using custom functions

Let us consider 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 in a few steps by just copy and paste the below script in our custom function. By adding an exit details for an employee, a workflow will be triggered where the mapped custom function will get executed. Upon successful execution selected employee's Employee status value will be changed as Resigned. Thus, the employee cannot log in to your organization account anymore.

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name
  • Provide Function Name
  • Provide the script as given below:

    getrecord = Map();

    pushrecord = Map();

    getrecord = zoho.people.getRecordById("employee",erecno);

    params = Map();

    params.put("authtoken","19f87d3e2f267887579e467935ac9c86");

    params.put("scope","creatorapi");

    params.put("Employee_ID",empid);

    params.put("First_Name",fname);

    params.put("Last_Name",lname);

     

    params.put("Email_ID",emailid);

     

    pushrecord = postUrl("https://creator.zoho.com/api/karenjohn/json/custom-function/form/Custom_form/record/add/",params);

    info pushrecord;

  • Click Save & Execute Script

Example scenario-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.

There are two ways to do this:

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:

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name
  • Provide Function Name as Auto-check Shift End Time
     
  • Provide the parameters as given below:

recordid = ZohoID(P_AttendanceForm)

erecno  = ZohoID(P_EmployeeForm)

  • Provide the script as given below:

getdata = Map();

getempdata = Map();

addrecord = Map();

tokenvalue= <authtoken>;

updaterecord = Map();

getdata = zoho.people.getRecordById("P_AttendanceForm",recordid.toLong());

expcheckout = getdata.get("ExpectedToTime");

checkout = getdata.get("ToTime");

getempdata = zoho.people.getRecordById("employee",erecno.toLong());

empid = getempdata.get("EmployeeID");

if(isBlank(checkout))

{

addrecord.put("authtoken",tokenvalue);

addrecord.put("empId",empid);

addrecord.put("checkOut",expcheckout);

updaterecord = postUrl("https://people.zoho.com/people/api/attendance",addrecord);

info updaterecord;

}

Output:

Checkout time = ExpectedToTime(Based on user shift)

  • Click Save and Execute script

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.

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name
  • Provide function name as Auto-check on 8hrs from Checked in
  • Provide the parameters as given below

recordid = ZohoID(P_AttendanceForm)

erecno  = ZohoID(P_EmployeeForm)

  • Provide the script as given below

getdata = Map();

getempdata = Map();

addrecord = Map();

tokenvalue = ;

updaterecord = Map();

getdata = zoho.people.getRecordById("P_AttendanceForm",recordid);

expcheckout = getdata.get("ExpectedToTime");

checkout = getdata.get("ToTime");

checkin = getdata.get("FromTime").toTime();

addedhrs= checkin.addHour(8);

getempdata = zoho.people.getRecordById("employee",erecno);

empid = getempdata.get("EmployeeID");

if(isBlank(checkout))

{

addrecord.put("authtoken",tokenvalue);

addrecord.put("empId",empid);

addrecord.put("checkOut",addedhrs.toString());

updaterecord = postUrl("https://people.zoho.com/people/api/attendance",addrecord);

info updaterecord;

}</authtoken>

Output:

Checkout time = Checkin time +8hrs

  • Click Save & Execute script

 

Example scenario-Automatic conversion of any currency to USD

Irrespective of the amount in the travel claim form, the currency will be converted to the USD based on the exchange rate of the day on which the claim is raised.

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name - i.e - Travel Claim form
  • Provide Function Name
  • Provide the parameters as given below:

recordid = ZohoID(Expense) here 'Expense' is the respective form name.

  • Provide the script as given below:

getValues = zoho.people.getRecordById("",recordId);

tabularsec = getValues.get("tabularSections").get("");

result = 0;

for each tabularsecvalues in tabularsec.toList()

{

cost = tabularsecvalues.get("");

format = tabularsecvalues.get("");

if(format != "USD" && cost > 0.00 && format != "")

{

currencyvalues = getUrl("http://api.fixer.io/latest?base=USD");

formatvalues = currencyvalues.get("rates").get(format);

totalval = cost.toDecimal() * formatvalues.toDecimal();

total = totalval.round(2);

result = total + result;

}

}

addrecmap = Map();

addrecmap.put("recordid",recordId);

addrecmap.put("total",result.toString());

addtotal = zoho.people.update("",addrecmap);

info addtotal;

 TipRefer here to know the form and field link names

  • Click Save and Execute Script

Example scenario-Automatic deduction of 1 day leave for 2 or more late entries

One day leave will be automatically deducted,if employee comes in late by an hour for more than one day

  • From your home page, go to Settings > Custom Functions under Automation > Add Custom Function
  • Select respective Form Name - i.e - Attendance form
  • Provide Function Name
  • Provide the parameter as given below

recordid = ZohoID(P_AttendanceForm)

  • Provide the script as given below:

getattendencerec = zoho.people.getRecordByID("P_AttendanceForm",recordid);

erecId = getattendencerec.get("EmployeeID.ID");

leavedate = getattendencerec.get("AttendanceDay");

expecfrmtime = getattendencerec.get("ExpectedFromTime");

frmtime = getattendencerec.get("FromTime");

if(frmtime.toDate() > expecfrmtime.toDate().addMinutes(15))

{

shiftmap = Map();

shiftmap.put("authtoken","authtoken");

shiftmap.put("sdate",leavedate.subDay(1));

shiftmap.put("edate",leavedate.subDay(1));

shiftmap.put("empId",empID);

getshiftconf = postUrl("https://people.zoho.com/people/api/attendance/getShiftConfiguration",shiftmap);

shiftdetails = getshiftconf.get("userShiftDetails").get("shiftList");

shiftdetailsmap = shiftdetails.get(0);

shiftstrttime = shiftdetailsmap.get("shiftStartTime");

shftime = leavedate.subDay(1) + " " + shiftstrttime;

t = shftime.toTime().addMinutes(15);

getreportmap = Map();

getreportmap.put("authtoken","authtoken");

getreportmap.put("sdate",leavedate.subDay(1).toString());

getreportmap.put("edate",leavedate.subDay(1).toString());

getreportmap.put("empId",empID);

getreports = postUrl("https://people.zoho.com/people/api/attendance/getUserReport",getreportmap);

s = leavedate.subDay(1).toString("yyyy-MM-dd");

Intime = getreports.get(s).get("FirstIn");

Intimestring = Intime.replaceAll(" [AP]M","") + ":00";

if(Intime.contains("PM"))

{

Intimestring = Intimestring.addHour(12);

}

if(Intimestring.toTime() > t)

{

shiftmap_1 = Map();

shiftmap_1.put("authtoken",);

shiftmap_1.put("sdate",leavedate.subDay(2));

shiftmap_1.put("edate",leavedate.subDay(2));

shiftmap_1.put("empId",empID);

getshiftconf2 = postUrl("https://people.zoho.com/people/api/attendance/getShiftConfiguration",shiftmap_1);

shiftdetails_1 = getshiftconf2.get("userShiftDetails").get("shiftList");

shiftdetailsmap_1 = shiftdetails_1.get(0);

shiftstrttime_1 = shiftdetailsmap_1.get("shiftStartTime");

shftime_1 = leavedate.subDay(2) + " " + shiftstrttime_1;

t_1 = shftime_1.toTime().addMinutes(15);

getreportmap_1 = Map();

getreportmap_1.put("authtoken","authtoken");

getreportmap_1.put("sdate",leavedate.subDay(2).toString());

getreportmap_1.put("edate",leavedate.subDay(2).toString());

getreportmap_1.put("empId",empID);

getreports_1 = postUrl("https://people.zoho.com/people/api/attendance/getUserReport",getreportmap_1);

s_1 = leavedate.subDay(2).toString("yyyy-MM-dd");

Intime_1 = getreports_1.get(s_1).get("FirstIn");

Intimestring_1 = Intime_1.replaceAll(" [AP]M","") + ":00";

if(Intime_1.contains("PM"))

{

Intimestring_1 = Intimestring_1.addHour(12);

}

if(Intimestring_1.toTime() > t_1)

{

details = Map();

details.put("authtoken","authtoken");

details.put("To",leavedate);

details.put("From",leavedate);

details.put("Leavetype",leave_type_ID);

details.put("Reasonforleave","due to late");

details.put("dateFormat","dd-MMM-yyyy");

result = postUrl("https://people.zoho.com/people/api/leave/addLeave",details);"

info result;

}

}

}

 TipRefer here to know the form and field link names

  • Click 'Save'

Example scenario-Increase leave balance by 1 day, if the over time is more than 2 hours a day

By making use of the below script, If the overtime per day is more than 2hours for an employee, then the initial balance can be increased by 1 day once in every 15days.

  • From your home page, go to Settings > Scheduler under Automation 
  • Click on 'Custom Scheduler' > 'Add Scheduler'
  • Provide a name for the Scheduler
  • Select the desired 'Start date and time' ,'Frequency' and 'Time zone'.
  • Provide the script as given below:

emp_map = Map();

emp_map.put("authtoken","");

emp_rec = postUrl("https://people.zoho.com/people/api/forms/P_EmployeeView/records",emp_map);

getrecmap = Map();

getrecmap.put("authtoken","");

getrecmap.put("sdate",today.subDay(15).toString("dd-MMM-yyyy"));

getrecmap.put("edate",today.subDay(1).toString("dd-MMM-yyyy"));

for each rec in emp_rec

{

empId = rec.get("EmployeeID");

getrecmap.put("empId",empId);

get_emp_rec = postUrl("https://people.zoho.com/people/api/attendance/getUserReport",getrecmap);

mapkeys = get_emp_rec.keys();

total_hours = 0;

total_min = 0;

for each mapkey in mapkeys

{

total = get_emp_rec.get(mapkey).get("TotalHours");

total_date_time = total.toTime("HH:mm");

hour = total_date_time.getHour();

total_hours = total_hours + hour;

minutes = total_date_time.getMinutes();

total_min = minutes + total_min;

}

min_to_hours = (total_min / 60).round(2);

result_hours = total_hours + min_to_hours;

if(result_hours >= 30)

{

addmap = Map();

addmap.put("authtoken","");

addmap.put("method","add");

addmap.put("userId",empId);

addmap.put("updateData","{'':'1'}");

addinitial = postUrl(""https://people.zoho.com/people/api/leave/updateInitialBalance",addmap);

info addinitial;

}

}

 TipRefer here to know the form and field link names

  • Click 'Save'

 TipThe above scripts can also be edited and used to serve other similar scenarios.

Share this post : FacebookTwitter

Still can't find what you're looking for?

Write to us: support@zohopeople.com