Help with Milkscript

I am trying to create a script to look at all tasks with the name "changeit" and remove all duplicates leaving the one with the farthest due date. I run this below script and nothing happens. Can you tell me what is wrong:
// MilkScript: Deduplicate tasks named "Changeit"
function main() {
var taskName = "Changeit"; // Task name to deduplicate
// Find all tasks with that name
var tasks = rtm.tasks.getList({
filter: 'name:"' + taskName + '"'
});
if (!tasks.tasks.length) {
return;
}
// Flatten task series into a single array
var allTasks = [];
tasks.tasks.forEach(function(list) {
list.forEach(function(taskseries) {
taskseries.task.forEach(function(task) {
allTasks.push({
list_id: list.id,
taskseries_id: taskseries.id,
task_id: task.id,
due: task.due ? new Date(task.due) : null
});
});
});
});
if (allTasks.length <= 1) {
return; // Nothing to deduplicate
}
// Sort by due date (farthest in the future first, nulls last)
allTasks.sort(function(a, b) {
if (!a.due && !b.due) return 0;
if (!a.due) return 1;
if (!b.due) return -1;
return b.due - a.due;
});
// Keep the first (farthest due), delete the rest
var keep = allTasks[0];
var toDelete = allTasks.slice(1);
toDelete.forEach(function(t) {
rtm.tasks.delete({
list_id: t.list_id,
taskseries_id: t.taskseries_id,
task_id: t.task_id
});
});
}
// MilkScript: Deduplicate tasks named "Changeit"
function main() {
var taskName = "Changeit"; // Task name to deduplicate
// Find all tasks with that name
var tasks = rtm.tasks.getList({
filter: 'name:"' + taskName + '"'
});
if (!tasks.tasks.length) {
return;
}
// Flatten task series into a single array
var allTasks = [];
tasks.tasks.forEach(function(list) {
list.forEach(function(taskseries) {
taskseries.task.forEach(function(task) {
allTasks.push({
list_id: list.id,
taskseries_id: taskseries.id,
task_id: task.id,
due: task.due ? new Date(task.due) : null
});
});
});
});
if (allTasks.length <= 1) {
return; // Nothing to deduplicate
}
// Sort by due date (farthest in the future first, nulls last)
allTasks.sort(function(a, b) {
if (!a.due && !b.due) return 0;
if (!a.due) return 1;
if (!b.due) return -1;
return b.due - a.due;
});
// Keep the first (farthest due), delete the rest
var keep = allTasks[0];
var toDelete = allTasks.slice(1);
toDelete.forEach(function(t) {
rtm.tasks.delete({
list_id: t.list_id,
taskseries_id: t.taskseries_id,
task_id: t.task_id
});
});
}
Log in
to post a reply.