Hello Everyone,
A Custom Function is a user-written set of code to achieve a specific requirement. Set the required conditions needed as to when to trigger using the Workflow rules (be it Tasks / Project) and associate the custom function to it.
Requirement:-
One of our customers wanted to update the work hours value same as log hours upon task completion. For example, if the work hours value is 8 and the log hours value is 5, then work hours should also be updated to 5 when the status of the Task is updated as closed. This ensures accurate user allocation, allowing the remaining available time to be assigned to other tasks. This was successfully implemented using Task custom functions in Zoho Projects.
Custom function code:-
getLogHours = invokeurl
[
url :endPoint + portalId + "/projects/" + projectId + "/tasks/" + taskId + "/logs/"
type :GET
connection:"XXXXXXX"
];
hours = getLogHours.get("timelogs").get("tasklogs");
logMap = Map();
for each logHours in hours
{
// info "logs : " + logHours.get("hours_display") + " owner_id : " + logHours.get("owner_id");
if(logMap.containsKey(logHours.get("owner_id"))) {
prev = logHours.get("hours_display").toList(":");
curr = logMap.get(logHours.get("owner_id")).toList(":");
sumFirst = prev.get(0).toLong()+curr.get(0).toLong();
sumSec = prev.get(1).toLong()+curr.get(1).toLong();
if(sumSec >= 60) {
sumFirst = sumFirst+(sumSec/60).round(0);
sumSec = sumSec-(sumSec/60).round(0)*60;
}
// info "sumFirst : "+sumFirst+" sumSec : "+sumSec;
logMap.put(logHours.get("owner_id"),sumFirst.toString()+":"+sumSec.toString());
} else {
logMap.put(logHours.get("owner_id"),logHours.get("hours_display"));
}
}
// info logMap;
taskResponse = zoho.projects.getRecordById(portalId,projectId,"Tasks",taskId,"XXXXXXX");
owners = taskResponse.get("tasks").get(0).get("details").get("owners");
// info owners;
taskWorks = List();
ownerIds = "";
for each owner in owners
{
if(owner.get("name").containsIgnoreCase("Unassigned"))
{
break;
}
else
{
isOwnerPresent = true;
ownerId = owner.get("id");
ownerWorkHours = logMap.get(ownerId);
taskWork = Map();
taskWork.put("user_id",ownerId);
taskWork.put("working_hours",ownerWorkHours);
taskWorks.add(taskWork);
ownerIds = ownerIds + owner.get("id") + ",";
// info ownerWorkHours;
}
}
taskOwners = ownerIds.removeLastOccurence(",");
// info taskOwners;
updateTaskParameter = Map();
if(isOwnerPresent)
{
updateTaskParameter.put("owner_work",taskWorks);
updateTaskParameter.put("work_type","work_hours");
updateTaskParameter.put("person_responsible",taskOwners);
updateTaskResponse = zoho.projects.update(portalId,projectId,"tasks",taskId,updateTaskParameter,"XXXXXXX");
info updateTaskResponse;
}
return "success";
Make sure to replace XXXXX with the Zoho Projects connection link name along with scopes ZohoProjects.portals.READ, ZohoProjects.tasks.ALL, ZohoProjects.timesheets.READ. Check
this link to learn how to create the connection.
Please find the screenshot of the parameters to be mapped and sample Task workflow rules. We hope you found this post useful. If you have any questions, feel free to share them in the comments below.