Help

Script automation

Topic Labels: Automations
196 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Guillaume_MIEMB
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello,

I wish to carry out the following project:

I have a table in Airtable named "Contenants" and another named "Collecte effectuée" ("Completed Collection" in English). In the "Contenants" table, all values are unique (there are no duplicates). I want to create an automation with the following trigger: when the "Collecte validée" ("Collection validated" in English) checkbox in the "Collecte effectuée" table is checked, it triggers a script. This script should allow me to filter the records in the "Contenants" table by only taking those whose "Numéro de collecte" ("Collection number" in English) cell value is equal to that of the "Numéro de collecte" cell of the "Collecte effectuée" table that I previously retrieved via an "input" variable.

For all the records thus retrieved in the "Contenants" table, it will filter them according to the value in the "Statut" column ("Disponible" (Available), "Déposé" (Deposited), or "Collecté" (Collected)) in the "Contenants" table. For each status, the script should tell me how many records I have by container types. I have managed to generate the code below but the result I obtain is not what I expected and I cannot debug it. Someone more knowledgeable than me can please help me because I am not very good at coding.

The script that I have written is as follows:

 

// Configuration des variables
let inputConfig = input.config();
let IdTrigger = inputConfig.IdTrigger;
let NumeroTrigger = inputConfig.NumeroTrigger;

// Récupérer la table Contenants
const contenants = base.getTable("Contenants");
const collect = base.getTable("Collecte effectuée");

// Créer des objets pour stocker les compteurs de volume par statut
const volumesDisponible = {
    "30 Litres": 0,
    "60 Litres": 0,
    "120 Litres": 0,
    "150 Litres": 0,
    "220 Litres": 0,
    "600 Litres": 0,
    "1000 Litres": 0,
    "Autres": 0
};

const volumesDepose = {
    "30 Litres": 0,
    "60 Litres": 0,
    "120 Litres": 0,
    "150 Litres": 0,
    "220 Litres": 0,
    "600 Litres": 0,
    "1000 Litres": 0,
    "Autres": 0
};

const volumesCollecte = {
    "30 Litres": 0,
    "60 Litres": 0,
    "120 Litres": 0,
    "150 Litres": 0,
    "220 Litres": 0,
    "600 Litres": 0,
    "1000 Litres": 0,
    "Autres": 0
};

// Définir les formules de filtre pour récupérer uniquement les enregistrements avec le même numéro de collecte et un statut spécifique
let filterDisponible = `{Numéro de collecte} = "${NumeroTrigger}" AND {Statut} = "Disponible"`;
let filterDepose = `{Numéro de collecte} = "${NumeroTrigger}" AND {Statut} = "Déposé"`;
let filterCollecte = `{Numéro de collecte} = "${NumeroTrigger}" AND {Statut} = "Collecté"`;

// Obtenir tous les enregistrements de la table Contenants avec le filtre et trier par "Numéro de collecte"
let queryResultDisponible = await contenants.selectRecordsAsync({
    filterByFormula: filterDisponible,
    sort: [{field: "Numéro de collecte", direction: "asc"}]
});

let queryResultDepose = await contenants.selectRecordsAsync({
    filterByFormula: filterDepose,
    sort: [{field: "Numéro de collecte", direction: "asc"}]
});

let queryResultCollecte = await contenants.selectRecordsAsync({
    filterByFormula: filterCollecte,
    sort: [{field: "Numéro de collecte", direction: "asc"}]
});

// Compter les volumes des contenants pour chaque statut
for (let record of queryResultDisponible.records) {
    let volume = record.getCellValue("Volume");
    switch (volume) {
        case 30:
            volumesDisponible["30 Litres"]++;
            break;
        case 60:
            volumesDisponible["60 Litres"]++;
            break;
        case 120:
            volumesDisponible["120 Litres"]++;
            break;
        case 150:
            volumesDisponible["150 Litres"]++;
            break;
        case 220:
            volumesDisponible["220 Litres"]++;
            break;
        case 600:
            volumesDisponible["600 Litres"]++;
            break;
        case 1000:
            volumesDisponible["1000 Litres"]++;
            break;
        default:
            volumesDisponible["Autres"]++;
            break;
    }
}

for (let record of queryResultDepose.records) {
    let volume = record.getCellValue("Volume");
    switch (volume) {
        case 30:
            volumesDepose["30 Litres"]++;
            break;
        case 60:
            volumesDepose["60 Litres"]++;
            break;
        case 120:
            volumesDepose["120 Litres"]++;
            break;
        case 150:
            volumesDepose["150 Litres"]++;
            break;
        case 220:
            volumesDepose["220 Litres"]++;
            break;
        case 600:
            volumesDepose["600 Litres"]++;
            break;
        case 1000:
            volumesDepose["1000 Litres"]++;
            break;
        default:
            volumesDepose["Autres"]++;
            break;
    }
}

for (let record of queryResultCollecte.records) {
    let volume = record.getCellValue("Volume");
    switch (volume) {
        case 30:
            volumesCollected["30 Litres"]++;
            break;
        case 60:
            volumesCollected["60 Litres"]++;
            break;
        case 120:
            volumesCollected["120 Litres"]++;
            break;
        case 150:
            volumesCollected["150 Litres"]++;
            break;
        case 220:
            volumesCollected["220 Litres"]++;
            break;
        case 600:
            volumesCollected["600 Litres"]++;
            break;
        case 1000:
            volumesCollected["1000 Litres"]++;
            break;
        default:
            volumesCollecte["Autres"]++;
            break;
    }
}

// Afficher les compteurs de volume pour chaque statut
console.log('Volumes pour le statut "Disponible":');
console.log(volumesDisponible);

console.log('Volumes pour le statut "Déposé":');
console.log(volumesDepose);

console.log('Volumes pour le statut "Collecté":');
console.log(volumesCollecte);

 

2 Replies 2

If you're open to exploring a non scripting option it seems like you could get similar functionality by having a linked field between your two tables with the "Collection number" as the linking value, and using rollups/counts/formula fields?

Best of luck with the scripting side of things


Thank you for taking the time to respond to me. Currently, I'm using a no-code method to do this. However, as part of a future certification for my company and in order to meet certain obligations, I'm required to change the entire structure of my database. Hence the desire to switch to a bit of code.