Help

Noobie trying to script a to-do list. Can't integrate dependencies between my tasks.

Topic Labels: Scripting
628 1
cancel
Showing results for 
Search instead for 
Did you mean: 
NicoB
5 - Automation Enthusiast
5 - Automation Enthusiast

Good morning to you,

I'm trying to create dynamic ToDoLists.
At first, I thought I could solve my problem by using complete NoCode (see my post).

Finally, after some advice, I turned to @Giovanni_Briggs code and post.

I've never coded the day before yesterday, and I'm trying to use AI tools to understand what I'm doing.

I've managed to adapt the proposed code to my base and half of what I want to do works.
As it happens, my code copies the tasks from my original list very well,
It links the new tasks to the event we've validated internally,
But it doesn't create a link between parent and child tasks (I've been tearing my hair out for 24 hours trying to solve this problem).

To explain, each parent task (main task) is linked to several subtasks (child tasks).
example : The Hotel task
is therefore linked to several subtasks:
Hotel search
Validation of a hotel according to quotation and price/quality ratio
Hotel reservation
Hotel payment
Receipt of invoice

When I use my script, everything works fine except for the dependencies part.
Once again, I saw this subject mentioned in Giovanni_Briggs' post.
But my limited knowledge of the subject prevents me from achieving what I want!

Thank you in advance for your time, I don't know what to try to solve my problem!

Here my code :

const tableTachesV3 = base.getTable('Tâche (V3)');

const tableTodoList = base.getTable('ToDo Lists');

// Fonction pour copier une tâche dans la Todo List

async function copierTache(NomDuCongres, RecordIDCongres, tache, tachesCopiees) {

  const nomTache = tache.getCellValue('Name');

  const categorie = tache.getCellValue('Catégories');

  // Création de la tâche dans la Todo List

  const tacheTodoList = await tableTodoList.createRecordAsync({

    'Name': nomTache,

    Catégories: categorie,

    'Lien vers congrès': [{ id: RecordIDCongres, name: NomDuCongres }],

  });

  // Stockez la correspondance entre la tâche d'origine et la copie

  tachesCopiees[tache.id] = tacheTodoList;

  return tacheTodoList;

}

// Fonction pour établir les dépendances entre les tâches de la Todo List

async function etablirDependances(tache, tachesCopiees) {

  const dependancesTacheV3 = tache.getCellValue('Records (Nested)'); // Assurez-vous que cela correspond à votre base de données

  if (dependancesTacheV3) {

    // Créez un tableau pour stocker les dépendances ToDo Lists

    const dependancesTodoList = [];

    // Parcourez les dépendances Tâche (V3) et mappez-les aux enregistrements ToDo Lists correspondants

    for (const dependance of dependancesTacheV3) {

      const dependanceName = dependance.name; // Nom de la dépendance dans Tâche (V3)

      // Trouvez la tâche ToDo Lists correspondante à partir du nom de la dépendance

      const dependanceTodoList = tachesCopiees[dependanceName];

      if (dependanceTodoList) {

        dependancesTodoList.push(dependanceTodoList);

      }

    }

    // Ajoutez les dépendances ToDo Lists à la tâche ToDo List

    if (dependancesTodoList.length > 0) {

      await tableTodoList.updateRecordAsync(tache, {

        'Records (Nested)': dependancesTodoList,

      });

    }

  }

}

// Fonction pour copier toutes les tâches d'un congrès dans la Todo List

async function copierToutesTachesDansTodoLists(NomDuCongres, RecordIDCongres, taches) {

  const tachesCopiees = {};

  // Copiez toutes les tâches dans la Todo List

  for (const tache of taches) {

    await copierTache(NomDuCongres, RecordIDCongres, tache, tachesCopiees);

  }

  // Établissez ensuite les dépendances

  for (const tache of taches) {

    await etablirDependances(tache, tachesCopiees);

  }

}

// Point d'entrée - Déclenché lorsqu'un congrès est validé

async function lorsqueCongresEstValide() {

  const inputConfig = input.config();

  const congresNom = inputConfig["Nom du congrès"];

  const congresRecordID = inputConfig["Record ID congrès"];

  const tachesV3 = await tableTachesV3.selectRecordsAsync();

  // Utilisez le nom du congrès passé en paramètre pour copier les tâches dans la Todo List

  await copierToutesTachesDansTodoLists(congresNom, congresRecordID, tachesV3.records);

}

// Point d'entrée - Déclenché lors de la validation d'un congrès

lorsqueCongresEstValide();
Thank you in advance for your time, I don't know what to try to solve my problem!
 
1 Reply 1
NicoB
5 - Automation Enthusiast
5 - Automation Enthusiast

Good afternoon,

I'm still trying to get my script to work.
In the meantime, I've added a "Parent Task" column and a "Child Task" column.

Below, my script works for the "Validated congress" automation, which copies my tasks from one table (master table) to another (ToDoLists), and links them to the validated congress. (I've translated the script into English to share it with you).
But there's no link between my tasks, whereas there is in the 1st table.

The automation works, the code is accepted by AirTable, but when I test the script, no link is made between the different tasks.

I've already pulled out half the hairs on my head in frustration, and I'd be enormously grateful if you could help me with this project!


const congressTable = base.getTable('📍 CONGRES');
const tasksV3Table = base.getTable('Tâche (V3)');
const todoListTable = base.getTable('ToDo Lists');

// Function to copy tasks from a congress into a Todo List
async function copyAllTasksToTodoLists(CongressName, CongressRecordID, tasks) {
const batchSize = 50; // Maximum batch size
const tasksToCreate = [];

// Iterate through all records in the Tâche V3 table
for (const task of tasks) {
// Copy data from each task to the Todo List
const taskName = task.getCellValue('Name');
const category = task.getCellValue('Catégories');

// Create the record for the Todo List
const newTodo = {
fields: {
'Name': taskName,
Catégories: category,
'Link to Congress': [{ id: CongressRecordID, name: CongressName }],
},
};

tasksToCreate.push(newTodo);

// If we've reached the maximum batch size, create that batch and reset the array
if (tasksToCreate.length >= batchSize) {
await todoListTable.createRecordsAsync(tasksToCreate);
tasksToCreate.length = 0;
}
}

// Create the last batch if there are any remaining records
if (tasksToCreate.length > 0) {
await todoListTable.createRecordsAsync(tasksToCreate);
}
}

// Function to establish links between tasks in the ToDo Lists table
async function linkTasksTogether(tasks) {
// Iterate through all records in the ToDo Lists table
for (const task of tasks) {
const parentTask = task.getCellValue('Tâche parente');
const childTask = task.getCellValue('Tâche enfant');

// If this task has a parent task, establish the link
if (parentTask) {
await todoListTable.updateRecordAsync(task.id, {
'Tâche parente': parentTask,
});
}

// If this task has child tasks, establish the links
if (childTask) {
await todoListTable.updateRecordAsync(task.id, {
'Tâche enfant': childTask,
});
}
}
}

// Entry point - Triggered when a congress is validated
async function whenCongressIsValidated() {
const inputConfig = input.config();
const congressName = inputConfig["Nom du congrès"];
const congressRecordID = inputConfig["Record ID congrès"];

const tasksV3 = await tasksV3Table.selectRecordsAsync();

// Use the provided congress name and record ID to copy tasks into the Todo List
await copyAllTasksToTodoLists(congressName, congressRecordID, tasksV3.records);

// Select all tasks from the ToDo Lists table
const allTasks = await todoListTable.selectRecordsAsync();

// Establish links between tasks
await linkTasksTogether(allTasks.records);
}

// Entry point - Triggered when a congress is validated
whenCongressIsValidated();