Scenario:
I don't want my employees to consume more than 2 Casual Leaves (CL) in a month on Zoho People. how do I validate that with the help of Custom function?
If the employee attempts to apply for the 3rd casual leave in that month then system will not allow to apply and display an error.
Example 1: Allowed
- Employee already took: 1 Casual Leave
- Applying again: 1 day
- Total for the month = 1 + 1 = 2 days → Allowed
Example 2: Not Allowed
- Employee already took: 2 Casual Leaves
- Applying again: 1 day
- Total for the month = 2 + 1 = 3 days → Not Allowed
- Error shown: “Only two leaves allowed per month.”
Example 3: Multi-day Leave Check
- Employee already took: 1 Casual Leave
- Applying now: 2 days leave (Ex: 10 Nov to 11 Nov)
- Total for the month = 1 + 2 = 3 days → Not Allowed
Example 4: Fresh Month
- Employee took 0 CL in this month
- Applying for 2 days
- Total = 2 → Allowed
Please find the below Deluge Script:-
errors = Collection();
leavetype = recordDetails.get("Leavetype.ID");
//info leavetype;
//---------------Please replace your Account Leave ID which you need to validate with "113505000000236060"---------
if(leavetype.equals("113505000000236060"))
{
paramsMap = Map();
paramsMap.put("from",from_date.toStartOfMonth().toString("dd-MMM-yyyy"));
paramsMap.put("to",to_date.eomonth(0).toString("dd-MMM-yyyy"));
paramsMap.put("unit","Day");
paramsMap.put("leavetype",{leavetype});
paramsMap.put("employee",{erecno});
paramsMap.put("approvalStatus",{"APPROVED","PENDING"});
res = invokeurl
[
url :"https://people.zoho.in/people/api/v2/leavetracker/reports/bookedAndBalance"
type :GET
parameters:paramsMap
connection:"peoplecf"
];
//info res;
if(!res.get("report").get(erecno).get(leavetype).get("booked").isNull())
{
totalCount = res.get("report").get(erecno).get(leavetype).get("booked").toLong();
// info "l:" + totalCount;
// info from_date.daysBetween(to_date).toLong() + totalCount;
if(from_date.daysBetween(to_date).toLong() + 1 + totalCount >= 3)
{
//info "ee";
errors.insert("global":"Only two leaves allowed per month.");
}
}
}
total_errors = Collection();
total_errors.insert("errors":errors);
return total_errors;
Note :
- "recordDetails" do not have to be defined in the function or parameter.
- "peoplecf" is the zoho people Connection Name replace it with your account Connection name.
- "from_date" is the parameter name you want to pass on function & map with Leave form 'From' Field.
- "to_date" is your parameter name pass from function & map it with Leave form 'To' Field.
- "erecno" is the Zoho People Employee ID.