@Megan_Eccleston,
First, a quick tip for the forum - when posting code, you can put the code inside triple-backticks (```) to keep the code formatted properly and make it easier to copy-paste code across the forum. So here’s your code cleaned up as I presume it is meant to be, and pasted back into the forum inside an opening and closing (```).
let districtTable = base.getTable("Districts(Education Agencies)");
let districtRecord = await districtTable.selectRecordsAsync();
let record = districtRecord.records[0]
let searchQuery = record.getCellValueAsString("Keywords");
let resultsField = districtTable.getField("Results");
for (let record of districtRecord.records){
async function searchAsync(searchTerm) {
let response = await fetch(
`https://api.apify.com/v2/actor-tasks/EFvVjhi1CSuOfd09L/run-sync-get-dataset-items?token=SsEpDxFwqJ7FgXL2TpN3EmicG`,
{
method: "POST",
headers: {"content-type": "application/json"},
body: JSON.stringify({queries: searchTerm}),
}
);
let result = await response.json();
let resultLines = [];
for (let {searchQuery, organicResults} of result) {
resultLines.push(`## ${searchQuery.term}`);
for (let {title, url, description} of organicResults) {
resultLines.push(`- [**${title}**:](${url}) ${description}`);
}
}
return resultLines.join('\n');
}
if (searchQuery) {
let results = await searchAsync(searchQuery.split(',').map(term => term.trim()).join('\n'));
await districtTable.updateRecordsAsync(record, {[resultsField.id]: results});
}
}
At the top of your script, you are:
-
Getting your districtTable.
-
Then you are getting all the records out of your districtTable and saving the Query result as districtRecord. This could presumably contain multiple records (all the records from the districtTable), but you have it named as a singular - this could be confusing (I might suggest renaming that to districtsQuery).
-
Next you are getting the first record from the Query result, and saving it as record.
-
Next, you are getting the value of the “Keywords” cell for that first districtTable record, named record, and saving that value as searchQuery
Following this, you have a loop that is looping through each record in the districtTable – for each record, it is checking if there is a value in the variable searchQuery, and if so, running your searchAsync() function with searchQuery as a value to pass to the apify API. However, you are setting the value of searchQuery only once, outside of your loop. For every single record you loop through, you are using the same value for searchQuery, namely, the value the of the “Keywords” field for the FIRST record in the districtTable. So every call to your searchAsync() function is using the same searchQuery.
I have a feeling this is not what you mean to do – I have a feeling you mean to retrieve the value of the “Keywords” field for each record as you loop over that record, and have that record’s “Keyword” value be the search query passed to the appify api. Is that right?
Hi Jeremy,
Thanks for the tip! Your assumptions are correct. I would like it to run through each record, using the search term within that record.
I got the code to work, running through one record at a time. Thanks!
let districtTable = base.getTable("Districts(Education Agencies)");
let districtsQuery = await districtTable.selectRecordsAsync();
let record = districtsQuery.records[0].id;
let resultsField = districtTable.getField("SearchResults");
output.text ("Searching....")
for (let record of districtsQuery.records){
let searchQuery = record.getCellValueAsString("Keywords");
if (searchQuery) {
let results = await searchAsync(searchQuery.split(',').map(term => term.trim()).join('\n'));
async function searchAsync(searchTerm) {
let response = await fetch(
`https://api.apify.com/v2/actor-tasks/EFvVjhi1CSuOfd09L/run-sync-get-dataset-items?token=SsEpDxFwqJ7FgXL2TpN3EmicG`,
{
method: "POST",
headers: {"content-type": "application/json"},
body: JSON.stringify({queries: searchTerm}),
}
);
let result = await response.json();
let resultLines = [];
for ( let {searchQuery, organicResults} of result) {
resultLines.push(`## ${searchQuery.term}`);
for ( let {title, url, description} of organicResults) {
resultLines.push(`- [**${title}**:](${url}) ${description}`);
}
}
return resultLines.join('\n');
}
await districtTable.updateRecordAsync(record, {SearchResults: results})
}
}
output.text ("search complete.")