Run script when records created and define a let for a unique person

I need to know how i ask to run script when new record is created and define if name in:

var response = await fetch('https://api.agify.io/?name='+name);

is equal to Prénom used from table else only change records only for this person.

I give you a screen of my table

// query for every record in "trombinoscope"

let table = base.getTable("test");
let view = table.getView("Donnée brut");
let query = await view.selectRecordsAsync
(
    {
    sorts: 
        [
        // sort by "Prénom" in ascending order...
        {field: "Prénom"},
        ]
    }
);

let records = query.records;
// print ID & "Prénom" from each record:
for (let record of query.records) 
{
    let name = (record.getCellValueAsString('Prénom')) ;
    let response = await fetch('https://api.agify.io/?name='+name);
    
    /*fetch('https://api.agify.io/?name='+name) ;*/

    /*console.log((await response.json()).age);*/
    let agifyAge = (( await response.json()).age);
    
    let records = query.records;
    for (let i=0 ; i<records.length ; i++ )
    {
        /*if ( name == ((await response.json()).name) )*/
        await table.updateRecordsAsync(
        [
            {
            id: records[i].id ,
            fields :
                {
                "Age estimé" : agifyAge ,
                },
            },
        ]);
    }
   
   /*console.log(await response.json());*/

    /*let json = JSON.parse(await response.json());
    console.log(json["age"]);*/
   
}

Create an automation that triggers when a record is created or when a record enters a view. I prefer the latter for better assurance that all the fields in the record are complete.

From this action, add a script action to capture the Prénom field value into variables from the previous step using the output feature.

// get the `Prénom` field value from the previous step
let inputConfig = input.config();
let thisPrénom = inputConfig.Prénom;

You are now ready to perform the fetch() call and test to see if it matches the captured thisPrénom value.

So i’m had to put
let inputConfig = input.config(); let thisPrénom = inputConfig.Prénom; in my for (let record of query.records) just before my fetch that right.

I’m sorry to ask this basic question but i’m learn my first time js :sweat_smile:.

No - your requirement is simpler:

  1. Capture the input of the record ID and Prénom field from the record create step;
  2. Query the external API using that input value to get the estimated age.
  3. Update the estimated age in the record whose ID was captured when it was created.

Done

1 Like