Power of Automation :: Automatically divide the work hours between the task owners

Power of Automation :: Automatically divide the work hours between the task owners

Hello Everyone,

A custom function is a software code that can be used to automate a process and this allows you to automate a notification, call a webhook, or perform logic immediately after a workflow rule is triggered. This feature helps to automate complex tasks and calculations. 

Let us consider the below use case from a customer - 

As per the design, Work hours will be allocated based on the number of owners assigned to a task. For example, A task is added with the duration of 16 hours and assigned to a user, John. So, the work hours allotted for him will be 16 hours. If one more owner is added to the same task, the work hours gets doubled and the total work hours will be 32. 

But we have received a specific use case from a customer and he wanted to allocate the work hours at task level instead of individual user level.

From the above scenario, he wants the work hours '16' to be equally divided between the task owners rather than doubling it to 32.

We resolved this use case using Custom functions in Zoho Projects

Todo : Create a connection for Zoho Projects service with the scopes “ZohoProjects.portals.READ”, “ZohoProjects.tasks.READ”, “ZohoProjects.tasks.UPDATE” to replace ‘connectionprojects’ with the connection name. Click this link below to learn how to create the connection. Attached the screenshots of Task Workflow Rule & Arguments.

Script to be used : 
  1. endPoint = "https://projects.zoho.com/restapi/portal/";
  2. updateTaskParameter = Map();
  3. ownerIds = "";
  4. isOwnerPresent = false;
  5. taskWorks = List();
  6. if((duration.contains(":")||duration.toNumber()!=0)&&affectedValues.get("action").containsIgnoreCase("Creation"))
  7. {
  8. // Get Task Details
  9. taskResponse = zoho.projects.getRecordById(portalId,projectId,"Tasks",taskId,"connectionprojects");
  10. owners = taskResponse.get("tasks").get(0).get("details").get("owners");
  11. workHours = taskResponse.get("tasks").get(0).get("work");
  12. workhour = workHours.toList(":").get(0).toNumber();
  13. workhour_mins = workHours.toList(":").get(1).toNumber();
  14. convertedMinsToHours = workhour_mins.toNumber() / 60;
  15. totalWorkHours = workhour + convertedMinsToHours;
  16. ownerCount = owners.size();
  17. for each  owner in owners
  18. {
  19. if(owner.get("name").containsIgnoreCase("Unassigned"))
  20. {
  21. break;
  22. }
  23. else
  24. {
  25. isOwnerPresent = true;
  26. ownerWorkHours = totalWorkHours / (ownerCount * ownerCount);
  27. taskWork = Map();
  28. taskWork.put("user_id",owner.get("id"));
  29. taskWork.put("working_hours",ownerWorkHours.round(2));
  30. taskWorks.add(taskWork);
  31. ownerIds = ownerIds + owner.get("id") + ",";
  32. }
  33. }
  34. taskOwners = ownerIds.removeLastOccurence(",");
  35. if(isOwnerPresent)
  36. {
  37. totalHours = totalWorkHours / ownerCount;
  38. updateTaskParameter.put("owner_work",taskWorks);
  39. updateTaskParameter.put("work_type","work_hours");
  40. updateTaskParameter.put("person_responsible",taskOwners);
  41. updateTaskResponse = zoho.projects.update(portalId,projectId,"tasks",taskId,updateTaskParameter,"connectionprojects");
  42. }
  43. }
  44. else
  45. {
  46. // Get Task Details
  47. taskResponse = zoho.projects.getRecordById(portalId,projectId,"Tasks",taskId,"connectionprojects");
  48. owners = taskResponse.get("tasks").get(0).get("details").get("owners");
  49. durationType = taskResponse.get("tasks").get(0).get("duration_type");
  50. duration = taskResponse.get("tasks").get(0).get("duration");
  51. if(durationType.containsIgnoreCase("hrs"))
  52. {
  53. totalHours = duration.toDecimal();
  54. }
  55. else
  56. {
  57. portalInfo = invokeurl
  58. [
  59. url :endPoint + portalId + "/"
  60. type :GET
  61. connection:"connectionprojects"
  62. ];
  63. businessHours = portalInfo.get("portals").get(0).get("settings").get("business_hours");
  64. business_start = businessHours.get("business_start");
  65. business_end = businessHours.get("business_end");
  66. businessTotalHours = (business_end - business_start) / 60;
  67. totalHours = duration.toDecimal() * businessTotalHours;
  68. }
  69. ownerCount = owners.size();
  70. for each  owner in owners
  71. {
  72. if(owner.get("name").containsIgnoreCase("Unassigned"))
  73. {
  74. break;
  75. }
  76. else
  77. {
  78. isOwnerPresent = true;
  79. ownerWorkHours = totalHours / ownerCount;
  80. taskWork = Map();
  81. taskWork.put("user_id",owner.get("id"));
  82. taskWork.put("working_hours",ownerWorkHours.round(2));
  83. taskWorks.add(taskWork);
  84. ownerIds = ownerIds + owner.get("id") + ",";
  85. }
  86. }
  87. taskOwners = ownerIds.removeLastOccurence(",");
  88. if(isOwnerPresent)
  89. {
  90. updateTaskParameter.put("owner_work",taskWorks);
  91. updateTaskParameter.put("work_type","work_hours");
  92. updateTaskParameter.put("person_responsible",taskOwners);
  93. updateTaskResponse = zoho.projects.update(portalId,projectId,"tasks",taskId,updateTaskParameter,"connectionprojects");
  94. }
  95. }
  96. return "success";
Watch this space for more such custom functions

      Zoho Developer Community









                                Zoho Desk Resources

                                • Desk Community Learning Series


                                • Digest


                                • Functions


                                • Meetups


                                • Kbase


                                • Resources


                                • Glossary


                                • Desk Marketplace


                                • MVP Corner


                                • Word of the Day



                                    Zoho Marketing Automation
                                            • Sticky Posts

                                            • Achieve Full Financial Control by Tracking Both Cost Budget and Budgeted Hours

                                              Zoho Projects now supports tracking budget with both Cost Budget and Budgeted Hours simultaneously across all modules (Based on Project, Phase, Task, or User). Why Track Both? Tracking both cost budget and budgeted hours provides a more holistic view
                                            • 🎉 Celebrate Zoho Projects’ 18th Birthday with Us! 🎉

                                              Hello Zoho Projects Community, We’re thrilled to announce that Zoho Projects is turning 18 today! To mark this milestone, we’re hosting a special AMA (Ask Me Anything) event starting at 3 PM IST today and running until 3 PM IST tomorrow. This is your
                                            • Zoho Projects Roadshow, USA - 2024

                                              Dear Users, We are happy to announce the Zoho Projects Roadshows 2024 in USA. This is an excellent opportunity to learn more about Zoho Projects and gain in-depth knowledge of the advanced features. Our team will also discuss industry specific solutions
                                            • Set Task Dependencies for Improved Project Management

                                              Task dependencies refers to the relationship between tasks, where the start or finish of one task relies on the start or finish of another. These dependencies are crucial for the successful completion of a project, as they help to ensure that all tasks
                                            • Project Billing with the Staff Hours Method in Zoho Projects

                                              The Staff Hours Billing Method in Zoho Projects allows you to bill your clients based on the actual time spent by each team member on a project, at the rate set for each user. This is useful for projects where different skill sets are needed and service


                                            Manage your brands on social media



                                                  Zoho TeamInbox Resources

                                                    Zoho DataPrep Resources



                                                      Zoho CRM Plus Resources

                                                        Zoho Books Resources


                                                          Zoho Subscriptions Resources

                                                            Zoho Projects Resources


                                                              Zoho Sprints Resources


                                                                Qntrl Resources


                                                                  Zoho Creator Resources


                                                                    Zoho WorkDrive Resources



                                                                      Zoho Campaigns Resources


                                                                        Zoho CRM Resources

                                                                        • CRM Community Learning Series

                                                                          CRM Community Learning Series


                                                                        • Tips

                                                                          Tips

                                                                        • Functions

                                                                          Functions

                                                                        • Meetups

                                                                          Meetups

                                                                        • Kbase

                                                                          Kbase

                                                                        • Resources

                                                                          Resources

                                                                        • Digest

                                                                          Digest

                                                                        • CRM Marketplace

                                                                          CRM Marketplace

                                                                        • MVP Corner

                                                                          MVP Corner





                                                                            Design. Discuss. Deliver.

                                                                            Create visually engaging stories with Zoho Show.

                                                                            Get Started Now