Hello,
I made a script to query a API and update current rankings of records. I have a test table with 5 records and 1 is a empty record.
See screenshot for my test table:
code:
//set the variables
let api_key = '{apikey}';
let table = base.getTable('tesdata');
let query = await table.selectRecordsAsync({fields: table.fields});
for (let record of query.records) {
let domain = record.getCellValueAsString('Website');
let zoekterm = record.getCellValue('Zoekterm');
let country = record.getCellValue('Land');
let gl=country;
let hl=gl;
if(gl=='co.uk'){
gl='uk';
hl='en';
}
else if (gl=='com'){
gl = 'us';
hl = 'en';
}
//build the api call variables
var url = 'https://serpapi.com/search?q='+encodeURIComponent(zoekterm)+ '&gl='+gl+'&hl='+hl+'&output=rank:'+domain+'&google_domain=google.'+country+'&num=100'
+ '&api_key=' + api_key;
console.log(url);
var requestOptions = {
method: 'GET',
};
//build the api call variables
//make api call
try{
var response= await fetch(url,requestOptions);
//make api call
const data = await response.json();
console.log(data)
await table.updateRecordAsync(record, {
// Change these names to fields in your base
'Hpos': data,
});
}catch(e){}
}
I have 2 issues with the script:
1)
The script works fine and returns all positions if I do console.log(data) without updating the actual Hpos fields.
However, if I add the update Hpos lines, script stops at the first row.
I need the script to update all rows that yield a complete query.
the table records seem to be loaded and processed in arbitrary mode. How can I make sure it follows the order of the tble displayed?
Record.id does not indicate the order.
What is wrong here and how to fix?
Thank you so much for troubleshooting with me.
-Anna