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

    Access your files securely from anywhere

        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

                                              • Enhanced Collaboration and Global Web Tabs

                                                Hello Users, We are rolling out two key enhancements in Zoho Projects that will be part of our November release. Here’s what to expect: 1. Collaboration Section in the Left Navigation Panel What’s new? All communication and interactive tools will be grouped
                                              • Tip 37: Time Log Restriction in Zoho Projects

                                                Timesheet in Zoho Projects helps you big time in entering log hours for the tasks and issues and approving them. Now, with the new Time Log Restriction option, you can set daily and weekly log hour limits. You can restrict users from entering extra log hours than the permissible limit. The limits are restricted to 24 hours per day and 168 hours per week by default based on business hours. To customize, navigate to Task & Timesheet settings under Portal Configuration in Zoho Projects setup and enable
                                              • Rename Task Module To Suit Your Project

                                                Task Module in Zoho Projects gives a comprehensive view of all the action items, known as Tasks, across all projects. However, different industries may use alternative terminology for similar processes, which might not always be recognised when labelled
                                              • 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
                                              • 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


                                              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 Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • 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