Help

Re: Assistance updating table from API

507 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Dave
5 - Automation Enthusiast
5 - Automation Enthusiast

I have a script that calls an API and writes the JSON objects to my table. This has hit the limit of what I know about scripting.

Can anyone help modify my script so that on the next call, it will update any records that exist and insert any records that are new.

I assume I need an IF statement to look for an existing ID and validate if the associated string matches ELSE create a new record.

My existing script is:
let response = await remoteFetchAsync(“APIURL”, requestOptions);
let json = await response.json();

let table = base.getTable(“Titles”);
for (i=0; i < json.length; i++) {
let recordId = await table.createRecordAsync({
“Title”: json[i].Title,
“ID”: json[i].ID
})
};

Thanks!

1 Reply 1

Welcome to the community, @Dave! :grinning_face_with_big_eyes: Here are a couple of tips that should get you a little further (if you haven’t already solved this).

First, I recommend switching your looping mechanism to use the for...of syntax:

for (let item of json) {
...

Then you would access properties of the item like this: item.Title. Unless there’s a need to use the index of the item in the original array, this is a more streamlined approach.

You’re definitely thinking in the right direction. I recommend looking into the find() array method, and using that to locate a matching record from the [Titles] table. If found, do what you want with the matching record; otherwise create a new one.