Print

Zoho Projects: Add a Time Log to an Issue using Deluge

What?
This is an article to demonstrate how to log a time event under a Zoho Projects Issue using code (Zoho Deluge) rather than the graphical user interface (GUI). Note this would probably be similar to when trying to enter a time against a project Task but this article is focused on getting this working against Project Issues. Also note, we will refer to Project Issues in this article, but in the backend code, Zoho refer to Issues as "Bugs".

Why?
At time of this article (Apr-2021), we could not find a working example that could allow us to do this. I have provided some links at the end of this article for you which we tried to use but only helped us out about half-way, the rest we guessed on our own:
Zoho Projects - Add Time Log to an Issue Using Deluge - Projects Issue Time Log
Our use case, is that we are developing a Zoho Creator app that will help staff log time and then push the creator record to Zoho Projects.

How?
For the following example, you will need to have setup a Zoho Oauth Connection with the appropriate authorized scopes and have access to all related systems such as Zoho Projects and the originating app (in this example, Zoho Creator).

1. Setup up Zoho Oauth Connection:
  1. Go to connections (in creator this is icon in top-left > Setup > Connections)
  2. Click on "Add Connection" and click on "Zoho OAuth"
  3. Give a lower connection name, eg. "joels_connector"
  4. Select the scope ZohoProjects.timesheets.ALL as a minimum (or CREATE/READ):
    Zoho Projects - Add Time Log to an Issue Using Deluge - Connection

2. Code to push Time Log entry to Zoho Projects:
  1. Example #1: Pushing a time log with a specified start date/time (requires end time field if using):
    copyraw
    //
    // init (from dropdowns you have made applicable to your Zoho Projects system)
    v_PortalID = ifnull(input.ZohoProject_Portal,0);
    v_ProjectID = ifnull(input.ZohoProject_Project,0);
    v_IssueID = ifnull(input.ZohoProject_Issue,0);
    //
    // get data from form
    v_StartTime = ifnull(input.Start_Time.toTime(),zoho.currenttime);
    v_EndTime = ifnull(input.End_Time.toTime(),zoho.currenttime.addHour(1));
    v_Billable = if(input.Billing == "Paid", "Billable", "Non Billable");
    v_WorkNotes = ifnull(input.Work_Notes,"");
    v_TaskOrIssue = if(input.Type == "Task", "tasks", "bugs");
    //
    // eval
    v_HoursDecimal = (v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60;
    v_TimeHour = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60).round(0).leftpad(2).replaceAll(" ", "0");
    v_TimeMinutes = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 % 60).round(0).leftpad(2).replaceAll(" ", "0");
    v_HoursHHmm = v_TimeHour + ":" + v_TimeMinutes;
    //
    // build map
    m_CreateTimeLog = Map();
    m_CreateTimeLog.put("date",v_StartTime.toDate().toString("MM-dd-yyyy"));
    m_CreateTimeLog.put("bill_status",v_Billable);
    m_CreateTimeLog.put("hours",v_HoursHHmm);
    m_CreateTimeLog.put("start_time",v_StartTime.toString("hh:mm a"));
    m_CreateTimeLog.put("end_time",v_EndTime.toString("hh:mm a"));
    m_CreateTimeLog.put("notes",v_WorkNotes);
    v_Url = "https://projects.zoho.eu/restapi/portal/"+v_PortalID+"/projects/"+v_ProjectID+"/"+v_TaskOrIssue+"/"+v_IssueID+"/logs/";
    r_CreateTimeLog = invokeurl
    [
    	url :v_Url
    	type :POST
    	parameters:m_CreateTimeLog
    	connection:"joels_connector"
    ];
    info r_CreateTimeLog;
    1.  // 
    2.  // init (from dropdowns you have made applicable to your Zoho Projects system) 
    3.  v_PortalID = ifnull(input.ZohoProject_Portal,0)
    4.  v_ProjectID = ifnull(input.ZohoProject_Project,0)
    5.  v_IssueID = ifnull(input.ZohoProject_Issue,0)
    6.  // 
    7.  // get data from form 
    8.  v_StartTime = ifnull(input.Start_Time.toTime(),zoho.currenttime)
    9.  v_EndTime = ifnull(input.End_Time.toTime(),zoho.currenttime.addHour(1))
    10.  v_Billable = if(input.Billing == "Paid", "Billable", "Non Billable")
    11.  v_WorkNotes = ifnull(input.Work_Notes,"")
    12.  v_TaskOrIssue = if(input.Type == "Task", "tasks", "bugs")
    13.  // 
    14.  // eval 
    15.  v_HoursDecimal = (v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60
    16.  v_TimeHour = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60).round(0).leftpad(2).replaceAll(" ", "0")
    17.  v_TimeMinutes = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 % 60).round(0).leftpad(2).replaceAll(" ", "0")
    18.  v_HoursHHmm = v_TimeHour + ":" + v_TimeMinutes; 
    19.  // 
    20.  // build map 
    21.  m_CreateTimeLog = Map()
    22.  m_CreateTimeLog.put("date",v_StartTime.toDate().toString("MM-dd-yyyy"))
    23.  m_CreateTimeLog.put("bill_status",v_Billable)
    24.  m_CreateTimeLog.put("hours",v_HoursHHmm)
    25.  m_CreateTimeLog.put("start_time",v_StartTime.toString("hh:mm a"))
    26.  m_CreateTimeLog.put("end_time",v_EndTime.toString("hh:mm a"))
    27.  m_CreateTimeLog.put("notes",v_WorkNotes)
    28.  v_Url = "https://projects.zoho.eu/restapi/portal/"+v_PortalID+"/projects/"+v_ProjectID+"/"+v_TaskOrIssue+"/"+v_IssueID+"/logs/"
    29.  r_CreateTimeLog = invokeUrl 
    30.  [ 
    31.      url :v_Url 
    32.      type :POST 
    33.      parameters:m_CreateTimeLog 
    34.      connection:"joels_connector" 
    35.  ]
    36.  info r_CreateTimeLog; 
  2. Example #2: Pushing a time log defaulting to the current time as start time:
    copyraw
    //
    // init (from dropdowns you have made applicable to your Zoho Projects system)
    v_PortalID = ifnull(input.ZohoProject_Portal,0);
    v_ProjectID = ifnull(input.ZohoProject_Project,0);
    v_IssueID = ifnull(input.ZohoProject_Issue,0);
    //
    // get data from form
    v_StartTime = ifnull(input.Start_Time.toTime(),zoho.currenttime);
    v_EndTime = ifnull(input.End_Time.toTime(),zoho.currenttime.addHour(1));
    v_Billable = if(input.Billing == "Paid", "Billable", "Non Billable");
    v_WorkNotes = ifnull(input.Work_Notes,"");
    v_TaskOrIssue = if(input.Type == "Task", "tasks", "bugs");
    //
    // eval
    v_HoursDecimal = (v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60;
    v_TimeHour = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60).round(0).leftpad(2).replaceAll(" ", "0");
    v_TimeMinutes = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 % 60).round(0).leftpad(2).replaceAll(" ", "0");
    v_HoursHHmm = v_TimeHour + ":" + v_TimeMinutes;
    //
    // build map
    m_CreateTimeLog = Map();
    m_CreateTimeLog.put("date",v_StartTime.toDate().toString("MM-dd-yyyy"));
    m_CreateTimeLog.put("bill_status",v_Billable);
    m_CreateTimeLog.put("hours",v_HoursHHmm);
    m_CreateTimeLog.put("notes",v_WorkNotes);
    v_Url = "https://projects.zoho.eu/restapi/portal/"+v_PortalID+"/projects/"+v_ProjectID+"/"+v_TaskOrIssue+"/"+v_IssueID+"/logs/";
    r_CreateTimeLog = invokeurl
    [
    	url :v_Url
    	type :POST
    	parameters:m_CreateTimeLog
    	connection:"joels_connector"
    ];
    info r_CreateTimeLog;
    1.  // 
    2.  // init (from dropdowns you have made applicable to your Zoho Projects system) 
    3.  v_PortalID = ifnull(input.ZohoProject_Portal,0)
    4.  v_ProjectID = ifnull(input.ZohoProject_Project,0)
    5.  v_IssueID = ifnull(input.ZohoProject_Issue,0)
    6.  // 
    7.  // get data from form 
    8.  v_StartTime = ifnull(input.Start_Time.toTime(),zoho.currenttime)
    9.  v_EndTime = ifnull(input.End_Time.toTime(),zoho.currenttime.addHour(1))
    10.  v_Billable = if(input.Billing == "Paid", "Billable", "Non Billable")
    11.  v_WorkNotes = ifnull(input.Work_Notes,"")
    12.  v_TaskOrIssue = if(input.Type == "Task", "tasks", "bugs")
    13.  // 
    14.  // eval 
    15.  v_HoursDecimal = (v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60
    16.  v_TimeHour = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 / 60).round(0).leftpad(2).replaceAll(" ", "0")
    17.  v_TimeMinutes = ((v_EndTime.toLong() - v_StartTime.toLong()) / 1000 / 60 % 60).round(0).leftpad(2).replaceAll(" ", "0")
    18.  v_HoursHHmm = v_TimeHour + ":" + v_TimeMinutes; 
    19.  // 
    20.  // build map 
    21.  m_CreateTimeLog = Map()
    22.  m_CreateTimeLog.put("date",v_StartTime.toDate().toString("MM-dd-yyyy"))
    23.  m_CreateTimeLog.put("bill_status",v_Billable)
    24.  m_CreateTimeLog.put("hours",v_HoursHHmm)
    25.  m_CreateTimeLog.put("notes",v_WorkNotes)
    26.  v_Url = "https://projects.zoho.eu/restapi/portal/"+v_PortalID+"/projects/"+v_ProjectID+"/"+v_TaskOrIssue+"/"+v_IssueID+"/logs/"
    27.  r_CreateTimeLog = invokeUrl 
    28.  [ 
    29.      url :v_Url 
    30.      type :POST 
    31.      parameters:m_CreateTimeLog 
    32.      connection:"joels_connector" 
    33.  ]
    34.  info r_CreateTimeLog; 
You will find I've included the Hours decimal calculation above but it's not used and just for future reference. The API wants the total hours and minutes in the format "HH:mm" (eg. "34:06" = 34 hours and 6 minutes).

Error(s)
Source(s)
Category: Zoho :: Article: 743