Help

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

1238 2
cancel
Showing results for 
Search instead for 
Did you mean: 
MEZIAT_Louis
4 - Data Explorer
4 - Data Explorer

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"]);*/
   
}
3 Replies 3

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 :grinning_face_with_sweat: .

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