javascriptgoogle-apps-scriptclockify

Text Cannot be Parsed to a Duration error from API


I'm using Google Apps Script to modify projects in Clockify (https://clockify.me/developers-api). I'm getting the error, "Text cannot be parsed to a duration".

Current code (note that the real goal is to recover the information from the current sheet's M3 cell, but for testing purposes I've commented that out):

function ClockifyEstimateUpdate() {
// Step 1: Find ProjectID for this file
  var sheet = ss.getActiveSheet();
  var FileNo = sheet.getSheetName();
  var url = 'https://api.clockify.me/api/v1/workspaces/'+cifyWorkspace+'/projects?name='+FileNo;
  var response = UrlFetchApp.fetch(url, cifyHeader);
  var json = response.getContentText();
  var data = JSON.parse(json);
  var PID = data[0]["id"];

//Step 2: Use M3 to Set Estimate
  //var estimate = sheet.getRange("M3");
  var estimate = '3000';
  var payload = JSON.stringify({'timeEstimate' : {'estimate' : estimate, 'type': "MANUAL", 'active': "true", 'resetOption': "null"}});
  //var payload = JSON.stringify({'timeEstimate' : {'estimate' : estimate, 'type': "MANUAL", 'active': "true", 'resetOption': "null"}, 'budgetEstimate' : {'estimate' : '0', 'type': "MANUAL", 'active': "false", 'resetOption': "null"}});
  var clockifyoptions = {
  'muteHttpExceptions' : true,
  'method' : 'patch',
  'headers' : cifyHeaders,
  'payload' : payload
  };
  var response2 = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/'+cifyWorkspace+'/projects/'+PID+'/estimate', clockifyoptions);
  Logger.log(response2);
}

Error as logged:

{"message":"Could not read document: Can not construct instance of com.clockify.adapter.http.project.TimeEstimateWithOptionsRequest, problem: Text cannot be parsed to a Duration\n at [Source: java.io.PushbackInputStream@44d0391a; line: 1, column: 89] (through reference chain: com.clockify.adapter.http.project.ProjectEstimateRequest["timeEstimate"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.clockify.adapter.http.project.TimeEstimateWithOptionsRequest, problem: Text cannot be parsed to a Duration\n at [Source: java.io.PushbackInputStream@44d0391a; line: 1, column: 89] (through reference chain: com.clockify.adapter.http.project.ProjectEstimateRequest["timeEstimate"])","code":3002}

I've tried setting estimate as '3000s' as has been proposed elsewhere. So far, this does not fix the problem. Do I need some sort of parsing of the estimate variable?

Thanks.


Solution

  • Got it. The answer was in the example in the Clockify API Documentation.

    The example called for a time estimate in the form "PT1H0M0S". This is ISO-8601. I needed to send my request in that format.