How to update a task in Todoist?

So I’m learning a bit here about how to use the Todoist stuff, but I am now stumped on a new task.

I have an array of strings that are task names. I’d like to take each task and give it a due date of tomorrow. I see @FlohGro has an action that will update the due date of an individual task, but I’m trying to build this function into a larger script, and I’m not sure how to break out this functionality from his massive Draftist actions.

I guess the questions would be, as I see the process:: how do I use the .updateTask method?

Something like this?

let to = Todoist.create()
to.apiVersion = 1;
let weekly = to.getTasks(
{"label_id": 2158923611}
)
for (w of weekly){
if(w.content == "drafting test"){
alert(w.content)
to.updateTask(w.id,{
	due_string: "tomorrow"
})
}
}

from the alert, I can see part of it is working at least. I can get the tasks from Todoist, and then it seems like I can get the ID of the desired task, but for whatever reason, the update part is not working. The due date is not changed for the Todoist task. I’ve tried wrapping “due_string” in quotes, but no luck there either.

This is the function that updates tasks in Draftist:

/**
 * Draftist_updateTask updates the provided task with the provided options
 * 
 * @param {Todoist_Object} todoist_obj? - if already created, otherwise the function will create its own.
 * @param {Todoist_Task} taskToUpdate - the task that should be updated
 * @param {String[]} labelNamesToRemove? - the valid label names which should be removed from the provided task
 * @param {String[]} labelNamesToAdd? - the valid label names which should be added to the provided task
 * @param {String} newDueDateString? - the new due date provided as String (best in the format YYYY-MM-DD; but Human defined dates are possible (https://developer.todoist.com/rest/v1/#update-a-task // https://todoist.com/help/articles/due-dates-and-times)
 * @param {String} newProjectName? - ATTENTION: currently 05/2022 not supported by the todoist API, providing this parameter will fail the action - the new valid project name for the provided task
 * @returns {Boolean} true when updated successfully, false when updating failed or any parameter was not valid (e.g. label name is not existing)
 */
function Draftist_updateTask({
  todoist = new Todoist(),
  taskToUpdate,
  labelNamesToRemove = [],
  labelNamesToAdd = [],
  newDueDateString = undefined,
  newProjectName = undefined
}) {
  if (!taskToUpdate) {
    Draftist_failAction("update task", "no task to update was provided")
    return false
  }
  const taskId = taskToUpdate.id;

  // update labels
  const currentLabels = taskToUpdate.label_ids;
  // init projectId variable

  // load todoist data if not already loaded and a relevant parameter is present & contains relevant values (e.g. labels, project id)
  if (labelsNameToIdMap.size == 0 && (labelNamesToRemove.length > 0 || labelNamesToAdd.length > 0 || newProjectName)) {
    Draftist_getStoredTodoistData();
  }

  // init labelId arrays
  let labelIdsToRemove = [];
  let labelIdsToAdd = [];

  for (labelName of labelNamesToRemove) {
    const curLabelId = labelsNameToIdMap.get(labelName);
    if (!curLabelId) {
      Draftist_failAction("update task", "provided label name \"" + labelName + "\" is not existing.");
      return false
    }
    labelIdsToRemove.push(curLabelId);
  }

  for (labelName of labelNamesToAdd) {
    const curLabelId = labelsNameToIdMap.get(labelName);
    if (!curLabelId) {
      Draftist_failAction("update task", "provided label name \"" + labelName + "\" is not existing.");
      return false
    }
    labelIdsToAdd.push(curLabelId);
  }

  let updatedLabelIds = labelIdsToAdd;

  for (curLabel of currentLabels) {
    // add the label to the updated array if its not already included and it is not contained in the labelsToRemove Array
    if (!labelIdsToRemove.includes(curLabel) && !updatedLabelIds.includes(curLabel)) {
      updatedLabelIds.push(curLabel);
    }
  }

  // update due date / date time

  let dueDateString = taskToUpdate.due_date;
  if (newDueDateString) {
    // new due date was provided
    dueDateString = newDueDateString
  }

  // attention the project ID was implemented based on the task property. 
  // turned out that project_id is not a parameter for the update task request: https://developer.todoist.com/rest/v1/#update-a-task
  // support request was sent on 2022-05-12 but until implementation this is a point of failure and will fail the function for now.
  let projectId = taskToUpdate.project_id;
  if (newProjectName) {
    // fail the action until project id is supported by Todoist:
    Draftist_failAction("update task", "new project name was provided but is currently not supported by Todoist")
    return false;
    projectId = projectsNameToIdMap.get(newProjectName);
    if (!projectId) {
      Draftist_failAction("update task", "provided project name \"" + newProjectName + "\" is not existing.");
      return false
    }
  }

  // create task options
  let options = {
    "content": taskToUpdate.content,
    "description": taskToUpdate.description,
    "project_id": projectId,
    "section_id": taskToUpdate.section_id,
    "parent_id": taskToUpdate.parent_id,
    "order": taskToUpdate.order,
    "label_ids": updatedLabelIds,
    "priority": taskToUpdate.priority,
    "due_string": (dueDateString ? dueDateString : undefined),
    "assignee": taskToUpdate.assignee
  };

  const updateTaskResult = todoist.updateTask(taskId, options);
  if (!updateTaskResult) {
    const lastError = Draftist_getLastTodoistError(todoist);
    if (lastError) {
      Draftist_failAction("update task", "todoist returned error: " + lastError)
    } else {
      Draftist_failAction("update task", "unknown error occured. please try again and contact @FlohGro with steps to reproduce")
    }
    return false;
  } else {
    return true;
  }

}

I would suggest to check the result of your call to: to.updateTask(…) - change it to result = to.updateTask(…); alert(result) if the result is false then you can check the last error with alert(to.lastError).

You can also try to use an iso date like „2022-09-25“ as due_string to check if this works.

Thanks for your help here. I just keep getting “undefined” as the result of the “updateTask” and that’s not enough for me to know how to fix. I’ve tried just putting in the ID number directly (for v1 api, it seems to want integers for IDs, not strings) but still “undefined.”

Did you check what alert(to.lastError) shows?

ah, that did it. I thought “lastError” was part of your functions. It turns out updateTask wants to be passed “content” as well, even if you’re not changing it.
THanks!

Sorry… I thought so in the first place but looked into their docs - seems like they are wrong here:
https://developer.todoist.com/rest/v2/#update-a-task